【发布时间】:2014-03-20 19:13:05
【问题描述】:
在下面的代码中,我试图让 Owner 表单带有一个下拉菜单,其中包含 Car 模型中的汽车品牌作为选项。我确实得到了下拉菜单,但每个元素都列为“汽车对象”,而不是品牌。如何将 Car 模型中的品牌添加到菜单中?谢谢。
models.py
from django.db import models
class Car(models.Model):
brand = models.CharField(max_length=20)
class Owner(models.Model):
name = models.CharField(max_length=20)
car_brand = models.ForeignKey(Car)
forms.py
from django.forms import ModelForm, ModelChoiceField
from app.models import Owner
class OwnerForm(ModelForm):
car_brand = ModelChoiceField(queryset=Car.objects.all())
class Meta():
model = Owner
【问题讨论】:
标签: python django django-models django-forms foreign-keys