如何将 Django 模型中的外键字段设置为 return 无值?
How do I set a foreignkey field in Django models to return no values?
我有一个像这样的 Django 模型:
class hotel(models.Model):
name = models.CharField(max_length=60)
city = models.ForeignKey('city', on_delete=models.DO_NOTHING)
state = models.ForeignKey('state', on_delete=models.DO_NOTHING)
country = models.ForeignKey('country', on_delete=models.DO_NOTHING)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "hotels"
class city(models.Model):
name = models.CharField(max_length=60)
state = models.ForeignKey('state', on_delete=models.DO_NOTHING)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "cities"
class state(models.Model):
name = models.CharField(max_length=60)
country = models.ForeignKey('country', on_delete=models.DO_NOTHING)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "states"
class country(models.Model):
name = models.CharField(max_length=60)
code = models.CharField(max_length=160)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "countries"
ModelForm 如:
class Hotel(ModelForm):
class Meta:
model = hotel
fields = '__all__'
[编辑] 当从 Hotel ModelForm 创建表单对象时,Django 自动填充外键字段的值。我想阻止它。
例如:
form = Hotel()
(Django 自动填充城市、州和国家/地区值)
我希望我的 Django 模板 return 下拉列表中的所有国家/地区值,但不是州和城市下拉列表(因此我希望州和城市下拉列表在 Django 模板中保持为空)。我该怎么做?
PS - 这样做的目的是首先加载国家值,根据用户选择的国家,我将相应地加载州和城市。
您可以在表单 __init__
方法中覆盖选项,或者您可以在定义字段后使用一些小技巧来设置选项
class Hotel(ModelForm):
class Meta:
model = hotel
fields = '__all__'
# Like this
state = forms.ModelChoiceField(state.objects.all())
state.choices = []
city = forms.ModelChoiceField(city.objects.all())
city.choices = []
# Or like this...
def __init__(self, data=None, *args, **kwargs):
super().__init__(data=data, *args, **kwargs)
if not data:
self.fields['state'].queryset = state.objects.none()
self.fields['city'].queryset = state.objects.none()
我有一个像这样的 Django 模型:
class hotel(models.Model):
name = models.CharField(max_length=60)
city = models.ForeignKey('city', on_delete=models.DO_NOTHING)
state = models.ForeignKey('state', on_delete=models.DO_NOTHING)
country = models.ForeignKey('country', on_delete=models.DO_NOTHING)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "hotels"
class city(models.Model):
name = models.CharField(max_length=60)
state = models.ForeignKey('state', on_delete=models.DO_NOTHING)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "cities"
class state(models.Model):
name = models.CharField(max_length=60)
country = models.ForeignKey('country', on_delete=models.DO_NOTHING)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "states"
class country(models.Model):
name = models.CharField(max_length=60)
code = models.CharField(max_length=160)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "countries"
ModelForm 如:
class Hotel(ModelForm):
class Meta:
model = hotel
fields = '__all__'
[编辑] 当从 Hotel ModelForm 创建表单对象时,Django 自动填充外键字段的值。我想阻止它。
例如:
form = Hotel()
(Django 自动填充城市、州和国家/地区值)
我希望我的 Django 模板 return 下拉列表中的所有国家/地区值,但不是州和城市下拉列表(因此我希望州和城市下拉列表在 Django 模板中保持为空)。我该怎么做?
PS - 这样做的目的是首先加载国家值,根据用户选择的国家,我将相应地加载州和城市。
您可以在表单 __init__
方法中覆盖选项,或者您可以在定义字段后使用一些小技巧来设置选项
class Hotel(ModelForm):
class Meta:
model = hotel
fields = '__all__'
# Like this
state = forms.ModelChoiceField(state.objects.all())
state.choices = []
city = forms.ModelChoiceField(city.objects.all())
city.choices = []
# Or like this...
def __init__(self, data=None, *args, **kwargs):
super().__init__(data=data, *args, **kwargs)
if not data:
self.fields['state'].queryset = state.objects.none()
self.fields['city'].queryset = state.objects.none()