我有一個輪詢應用程序與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**