Django, auto setting a field during a save, based on other Admin page inputs

后端 未结 1 1355
别那么骄傲
别那么骄傲 2021-01-17 03:24

I\'m looking for the correct way to set full_name in SuperPerson instance.

class Suffix(models.Mode):
    suffix = models.CharField(max_length=255)
    def _         


        
相关标签:
1条回答
  • 2021-01-17 03:56

    The problem is your m2m relationship with Suffix, or rather the way that django admin saves m2m relationships.

    A pretty good explanation is in this answer to Why is adding site to an object doesn't seem to work in a save() override in the Django admin?

    When you save a model via admin forms it's not an atomic transaction. The main object gets saved first (to make sure it has a PK), then the M2M is cleared and the new values set to whatever came out of the form.

    post_save() is actually still too early. That's where the instance was saved, not its relationships.

    You need to connect to the m2m_changed signal: https://docs.djangoproject.com/en/dev/ref/signals/#m2m-changed

    or wait for Django 1.4 where ModelAdmin gives you a "when all is done" signal: https://code.djangoproject.com/ticket/16115

    0 讨论(0)
提交回复
热议问题