在下面的代碼中,我試圖讓所有者形成一個包含汽車品牌作爲選項的下拉菜單。我確實得到了下拉菜單,但每個元素都列爲「汽車物體」,而不是品牌。我如何從汽車模型中的品牌進入菜單?謝謝。Django從ForeignKey填充選項
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