Django, auto setting a field during a save, based

2019-02-21 02:12发布

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 __unicode__(self):
        return u'%s'%(self.suffix)

class Person(models.Model):
    first_name= models.CharField(max_length=255)
    last_name= models.CharField(max_length=255)
    suffixes= models.ManyToManyField(Suffix, blank=True, null=True)
    full_name= models.CharField(max_length=255)

class SuperPerson(Person):
    ignore_this_field= model.CharField(max_length=255)

full_name is hidden from the user on the Admin page, and is to be automatically be updated based on the other inputs on Admin page when the Admin page save button is hit.

I have tried overriding save like this and variations:

def save(self, *args, **kwargs):
    # Attempt to get data into the database so I can access it
    super(SuperPerson,self).save(*args,**kwargs)

    self.full_name = self.first_name + self.last_name 
    for suf in self.suffixes.all():
        self.full_name+= suf.__unicode__()

    # Now save the copy with full_name set as I wish
    super(SuperPerson,self).save(*args,**kwargs)

This method works if I hit the save button in the Admin page twice, which is unacceptable for my use cases, seems like the new self.suffixes I have entered from the Admin page hasn't made it into database with the first super.save when I call self.suffixes.all().

I tried making full_name a property with decorator, but I also need to be able to filter Person and SuperPerson dbs using full_name, so that didn't work, unless someone can tell me how to filter with a property. Though I would rather have the value saved in the db.

I tried pre_save and post_save signals - neither worked.

@receiver(pre_save, sender=SuperPerson)
def set_full_name(sender, instance, **kwargs):
    instance.full_name = instance.first_name + instance.last_name

    for suf in instance.suffixes.all():
        instance.full_name+= ', ' + suf.__unicode__()

Edit: - this has same effect - the instance suffixes do not match what was in the Admin page.

What is the right way to save full_name based on other inputs? Oh and I'm hoping to avoid messing with Admin forms.

ADDITIONAL INFORMATION: It seems the problem is specifically that the suffixes field is not being updated by the time I'm trying to use it. I can update full_name to something else, like appending a string representing the current date, I just cannot access the suffixes.

Thanks, Dale

SOLUTION:

@receiver(m2m_changed, sender=Person.suffixes.through)
def set_full_name_after_ManyToMany_saved(sender, instance, **kwargs):
    instance.full_name = instance.first_name + instance.last_name
    for suf in instance.suffixes.all():
        instance.full_name+= ', ' + suf.__unicode__()
    print 'Saving As', instance.full_name
    instance.save()

I'm curious why I had to use Person.suffixes.through instead of SuperPerson, Suffixes or Person.suffixes - is there good documentation on this somewhere, I couldn't find it. And, it runs the code 4 times, but at least ends up with the correct result in the final run.

Many thanks to Danny and burhan

1条回答
Anthone
2楼-- · 2019-02-21 02:42

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

查看更多
登录 后发表回答