我有这些模型:显示的ModelForm填充组合框由一个PK过滤
class LocationType(models.Model):
Name=models.CharField(max_length=50)
Description=models.CharField(max_length=200)
class Location(models.Model):
Name=models.CharField(max_length=100)
ParentCode=models.BigIntegerField()
LocationType=models.ForeignKey(LocationType)
class Visa(models.Model):
Country=models.ForeignKey(Location)
Price=models.CharField(max_length=12)
ActionUser=models.ForeignKey(User,editable=False)
在forms.py
我有这样的:
class VisaForm(ModelForm):
class Meta:
model=Visa
我要显示的位置的组合框(为Visa模式的国家/地区)以签证形式提供,并由特殊的LocationType
过滤。
想象一下,我有一个LocationType
价值name=Country,pk=1
。在visa_form
我想显示Location
s列表,但不是全部。只需地址为locationType_id=1
。
如何填写此组合框并以签证形式显示?
也许['ModelChoiceField'](https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield)会更适用于有问题的情况。 –
感谢U everybodey,我这样做,它的工作原理:在__init__ self.fields [“Country”]。queryset = Location.objects.filter(LocationTypeCode__id = locationType.id) –