【发布时间】:2015-02-08 19:06:15
【问题描述】:
我有一个投票应用程序,在投票模型中有一个多对多字段。
我想检索视图中选择(ManyToMany)字段的值。
models.py
class Poll(models.Model):
question = models.CharField(max_length=250)
pub_date = models.DateTimeField()
end_date = models.DateTimeField(blank=True, null=True)
choice= models.ManyToManyField(Choice)
def __unicode__(self):
return smart_unicode(self.question)
class Choice(models.Model):
name = models.CharField(max_length=50)
photo = models.ImageField(upload_to="img")
rating = models.CharField(max_length=10)
def __unicode__(self):
return smart_unicode(self.name)
views.py 每个投票中有 2 个选项,我想将这些选项中的每一个分配给 2 个单独的变量,但不知道如何分配。
def polling(request)
try:
choices = Poll.objects.all()
choice_1 = **assign 1st ManyToMany Field value**
choice_2 = **assign 2nd ManyToMany Field value**
【问题讨论】:
标签: python django django-models django-templates django-views