2

当评论类中出现条目时,我们如何增加计数器?

class Status(models.Model):
    status = models.CharField(max_length=140)
    counter = models.IntegerField(default=0)

class Comments(models.Model):
    status = models.ForeignKey(status)
    comment = models.CharField(max_length=140)
4

2 回答 2

4

通过使用信号或将其添加到(保存)注释的处理方法中。

于 2012-10-31T10:09:38.297 回答
0

您可以覆盖您的保存方法

class Comments(models.Model):
   status = models.ForeignKey(status)
   comment = models.CharField(max_length=140)

   def save(self, *args, **kwargs):
         #get the status 
         #save it

         #Call super save to store your comment
         super(Comments, self).save(*args, **kwargs)
于 2012-10-31T10:10:22.923 回答