On Mon, 2009-03-16 at 08:48 -0700, Brandon Taylor wrote: > Hi everyone, > > I'm running Python 2.6.1, Django Trunk. > > I have a model (Entry) with a ManyToMany (Categories). I need to be > able to iterate over these categories whenever my parent model is > saved. > > I've tried overriding save_model in the admin, and adding a post_save > signal to Entry in order to be able to iterate over the categories. > The problem only occurs on an insert. If I do: > > class Entry(models.Model): > name = models.CharField(max_length=100) > categories = models.ManyToManyField(Category) > > class Meta: > verbose_name_plural = 'Entries' > > def __unicode__(self): > return self.name > > class EntryAdmin(admin.ModelAdmin): > def save_model(self, request, obj, form, change): > obj.save() > print obj.categories.all() > > obj.categories.all() = [] > > However, when performing an update, I have a list of Category objects. > Is this a model lifecycle issue as far as when the ManyToMany gets > saved? What can I do to be able to access the list of categories when > the object is created?
Many-to-many relations cannot be saved until the object itself is saved, since they need ot know the object's pk value to use in the relation table. Consequently, many-to-many handling in forms is done after saving the model itself. You're trying to look up the categories before they've been saved. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---