> Sure. I'm trying to set a boolean property on the object based on the > existence (or absence) of a ManyToMany relationship. So I originally > thought something like: > > def save(self): > self.has_m2m_thing = bool(self.m2m_relation.count()) > super(MyModel, self).save() > > This works fine if you save it twice, but that's not the behavior I > was looking for :).
Right. How about something along these lines (a lazy initialization pattern): 1. Change MyModel's has_m2m_thing to m2m_count and make it an integer field and default it to -1. 2. Add a method and property to MyModel: def _has_m2m_thing(self): if self.m2m_count == -1: # compute count self.m2m_count = self.m2m_relation.count() super(MyModel, self).save() return self.m2m_count > 0 has_m2m_thing = property(_has_m2m_thing) 3. Add this to MyModel.save() def save(self): self.m2m_count = -1 # reset count super(MyModel, self).save() Caveat: This doesn't work if you add or remove your m2m objects outside of the Admin (because MyModel.save() won't get called so the m2m_count won't get reset.) You will have to explicitly reset m2m_count in such cases by calling MyModel.save(). --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---