faijan memon
faijan memon

Reputation: 203

save method in django views.py not working after overriding it in models,py

this is my home function from views.py file, I am trying to update the count of comments on respective blogs if anyone have commented on my blog post. everything was working fine before but then I added save method in models.py and from then even if i change keywords of blog from admin panel and tries to save it , it saves but don't update the keywords and keeps the previous one. [ i printed every blog and their respective comments and they are printing the right result suppose my blog 1 had 2 comments and someone added new comment i get three as comment count for blog 1 ] Can someone please tell me what's the issue and help me to solve it.

 def home(request):
    all_blogs = Blog.objects.all()
    for b in all_blogs:
        comment_per_blog = Comment.objects.filter(blog=b.id, active=True).count()
        print(f"blog {b.id} has {comment_per_blog} comment")
        b.blog_comments = comment_per_blog
        b.save()

This is my blog model from the models.py file.

  class Blog(models.Model):
        objects = models.Manager()
        slug = models.SlugField(default="", null=True, blank=True, max_length=255)
        keywords = models.CharField(max_length=500, default="", null=True, blank=True)
        title = models.CharField(max_length=500)
        main_image = models.ImageField(upload_to='Blog', null=True, blank=True)
        category = models.ForeignKey(Category, on_delete=models.CASCADE)
        body = RichTextUploadingField(null=True, blank=True)
        tags = models.CharField(max_length=255)
        author = models.ForeignKey(User, on_delete=models.CASCADE)
        created = models.DateTimeField(auto_now_add=True)
        likes = models.IntegerField(default=0)
        blog_comments = models.IntegerField(default=0)
        active = models.BooleanField(default=True)

        def save(self, *args, **kwargs):
            if self.slug is None:
               year = datetime.today().year
               self.slug = slugify(str(year) + '-' + str(self.category) + '-' + str(self.title))
               super(Blog, self).save(*args, **kwargs)
    
        def __str__(self):
            return self.title

my comment class, this class stores the comments for the each blogs.

class Comment(models.Model):
    blog = models.ForeignKey(Blog,on_delete=models.CASCADE,related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField()
    body = models.TextField()
    created_on = models.DateTimeField(default=datetime.now())
    active = models.BooleanField(default=True)

class Meta:
    ordering = ['created_on']
    verbose_name = 'Blogs Comment'
    verbose_name_plural = 'Blogs Comment'
    
def __str__(self):
    return ' {} by {}'.format(self.body, self.name)

Upvotes: 1

Views: 467

Answers (1)

Rafael Gruhn
Rafael Gruhn

Reputation: 48

The self.slug has default = "" so in the save method it doesn’t enter the condition, so the super doesn’t work, I recommend modifying the save method for that:

        def save(self, *args, **kwargs):
            if self.slug is None or self.slug == "":
                year = datetime.today().year
                self.slug = slugify(str(year) + '-' + str(self.category) + '-' + str(self.title))
            super(Blog, self).save(*args, **kwargs)

Upvotes: 1

Related Questions