Reputation: 13
I want to alter the count of a ManyToMany Field during save of my model. For which I modified the save(). If I run save(), count will not be updated. If I run save second time without updating the field it will update the count.
class UserProfile(models.Model):
follower = models.ManyToManyField('self', related_name='Followers',
blank=True, symmetrical=False)
following = models.ManyToManyField('self', related_name='Followings',
blank=True, symmetrical=False)
follower_count = models.PositiveIntegerField(null=True, blank=True,
editable=False)
following_count = models.PositiveIntegerField(null=True, blank=True,
editable=False)
class Meta:
verbose_name = 'User Profile'
verbose_name_plural = 'User Profiles'
def __str__(self):
return self.follower_count
def save(self, *args, **kwargs):
super(UserProfile, self).save(*args, **kwargs)
self.following_count = self.following.all().count()
self.follower_count = self.follower.all().count()
super(UserProfile, self).save(*args, **kwargs)
I want to update count with save().
Upvotes: 1
Views: 327
Reputation: 32274
If performance is not an issue both fields can be turned into properties that calculate the value on every access
class UserProfile(models.Model):
follower = models.ManyToManyField('self', related_name='Followers',
blank=True, symmetrical=False)
following = models.ManyToManyField('self', related_name='Followings',
blank=True, symmetrical=False)
@cached_property
def follower_count(self):
return self.follower.count()
@cached_property
def following_count(self):
return self.following.count()
Upvotes: 0
Reputation: 477190
You can omit the first save, like:
def save(self, *args, **kwargs):
self.following_count = self.following.count()
self.follower_count = self.follower.count()
super(UserProfile, self).save(*args, **kwargs)
That being said, I'm not entirely sure that the above is a good idea for several reasons:
Upvotes: 1
Reputation: 1196
def save(self, *args, **kwargs):
self.following_count = self.following.all().count()
super(UserProfile, self).save(*args, **kwargs)
Get the count first and then call save method
Upvotes: 1