I want to count tags . I have Post model and tags model and post model has manytomany field to tags. When I want to add new post meanwhile I add tags for post , it has to increase tag count number.
My Post and Tags model:
class Post(models.Model):
tags = models.ManyToManyField(Tag,blank=True,null=True,verbose_name=_('tags'))
def save(self)
super(Post, self).save()
for i in self.tags.all():
i.save()
class Tag(models.Model):
name=models.CharField(max_length=30,verbose_name=_('name'))
count = models.IntegerField(blank=True,null=True,default=0)
slug = models.SlugField(blank=True,null=True)
def save(self):
self.slug = slugify(self.name.upper())
self.count = Post.objects.filter(tags__name=self.name).count()
super(Tag, self).save()
When I added new post , I look up the tag model and see count number does not change but when I press the save button in Tags Admin then I see the real count. But I want to see real count when I look up the tag admin without pressing the save button in tag.
Also I want that when I edit the post it should not increase the tags count again.