django post_save signal and ManyToManyField (and Django Admin) -


i have problem post_save function. function correctly triggered instance doesn't contains value insereted. checked function using ipdb , there nothing wrong. manytomanyfield empty.

the code:

@receiver(post_save, sender=supplier) def set_generic_locations(sender, instance, **kwargs):     """ set generic locations new created supplier.     """     created = kwargs.get('created')     if created:         glocations = locationaddress.get_generic_locations()          location in glocations:             instance.locations.add(location)          instance.save() 

the field used in instance:

locations = models.manytomanyfield(locationaddress, blank=true)​ 

i don't understand why, locations empty.

i use django 1.8.8

update

the problem django admin. found explanation here: http://timonweb.com/posts/many-to-many-field-save-method-and-the-django-admin/

the code solve problem in django admin

def save_related(self, request, form, formsets, change):     super(supplieradmin, self).save_related(request, form, formsets, change)     form.instance.set_generic_locations() 

manytomanyfields work little bit differently signals because of difference in database structures. instead of using post_save signal, need use m2m_changed signal


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -