continuing the Django polls tutorial, I want an entry (I called it numChoices) in the Poll model that automatically updates with the number of Choices associated with that Poll. How can I do that?
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
numChoices = models.IntegerField()
def __unicode__(self):
return self.question
def was_published_recently(self):
now=timezone.now()
return now-datetime.timedelta(days=1) <= self.pub_date < now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.choice_text
Clarification, the ideal use case would be such that:
If i just want to list all the polls with the number of choices associated with each poll, it won't have to query the Choice table.
But every time i add a choice, the poll entry associated with that choice will update it's numChoices count