我有一个轮询应用程序与Poll模型中的ManytoMany字段。django的意见 - 获取ManyToMany字段的值
我想检索视图中的选项(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**