Let's say I have such model
class Event(models.Model)
users_count = models.IntegerField(default=0)
users = models.ManyToManyField(User)
How would you recommend to update users_count value if Event add/delete some users ?
Let's say I have such model
class Event(models.Model)
users_count = models.IntegerField(default=0)
users = models.ManyToManyField(User)
How would you recommend to update users_count value if Event add/delete some users ?
There is a "design pattern" for that.
Pretty much, override save()
and delete()
.
As they mention, using listeners is also an option, but for this scenario I tend to like the override mechanism better.
Overriding save() may not help you because the update to the M2M is not atomic and happens after the save of the Event instance (I haven't studied the delete() semantics but they are probably similar). This was discussed in another thread.
People are talking about and working on this problem. The best solution I have seen so far is this MonkeyPatch by gregoirecachet. I don't know if this will make it into 1.2 or not. Probably not since the Release Manager (James Bennett) is trying to get people to respect the freeze dates (a major one just passed).
If possible in your case, you could introduce Participation
model which would join Event and User:
class Participation(models.Model):
user = models.ForeignKey(User)
event = models.ForeignKey(Event)
class Event(models.Model):
users = models.ManyToManyField(User, through='Participation')
And handle pre_save signal sent by Participation
to update instance.event
counts. It would simplify handling of m2m significantly. And in most cases, it turns out later on that some logic and data fits best in the middle model. If that's not your case, try a custom solution (you should not have many code paths adding Users to Events anyway).