【发布时间】:2016-01-16 15:58:49
【问题描述】:
我有 models.py 为:
class FoodCategory(models.Model):
category = models.CharField(max_length = 50)
content = models.CharField(max_length= 50, null = True,blank=True)
preparation = models.CharField(max_length= 50, null=True, blank=True)
time = models.CharField(max_length=50,null=True, blank=True)
def __str__(self):
return '%s %s %s %s' % (self.category, self.content, self.preparation, self.time)
现在我从 django 管理站点为 FoodCategory 填写了一些值。我需要将这些值显示为下拉字段,即类别的下拉字段、内容的另一个下拉字段以及准备和时间的类似下拉字段。
我的forms.py如下:
class FoodForm(forms.ModelForm):
category = forms.ModelChoiceField(queryset=Category.objects.all())
time = forms.ModelChoiceField(queryset=Category.objects.all())
preparation = forms.ModelChoiceField(queryset=Category.objects.all())
content = forms.ModelChoiceField(queryset=Category.objects.all())
class Meta:
model = FoodItems
fields = ('name','time', 'category', 'content', 'preparation', 'comment',)
但现在所有下拉字段都显示为:
我需要将 Starter-Soup、Veg、American、Breakfast 分别分类、内容、准备、时间
所以我认为问题在于__str__ 的返回值。如何单独退货?
【问题讨论】:
标签: python django django-forms