Tojo Cherian
Tojo Cherian

Reputation: 33

Django: increment the counter of another model when an entry occurs

how can we increment the counter when an entry occurs in the comments Class???

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)

Upvotes: 2

Views: 222

Answers (2)

Jay
Jay

Reputation: 2569

You can override your save method

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)

Upvotes: 0

Hedde van der Heide
Hedde van der Heide

Reputation: 22469

Either by using a signal or adding it to the processing method of (saving) comments.

Upvotes: 4

Related Questions