Django模型选择字段:大量的选择列表

时间:2021-11-21 02:04:47

Consider these 3 models:

考虑这三个模型:

# models.py
class City(models.Model):
    name = models.CharField(max_length=50)

class Institute(models.Model):
    name = models.CharField(max_length=100)
    city = models.ForeignKey(City)

class Student(models.Model):
    name = models.CharField(max_length=60)
    roll = models.CharField(max_length=10)
    institute = models.ForeignKey(Institute)

Now I am creating a model form to add/edit students but I provide the option to select city and institute to the user. So basically when a user selects city the corresponding institutes are displayed using JavaScript

现在我正在创建一个模型表单来添加/编辑学生,但是我提供了选择城市和学院给用户的选项。基本上,当用户选择城市时,相应的机构会用JavaScript显示出来

# forms.py
class StudentForm(forms.ModelForm):
    city = forms.ModelChoiceField(City)
    institute = forms.ModelChoiceField(queryset=Institute.objects.all())

class Meta:
    model = Student

But the number of institutes is over 3000. So what I want is to keep the choices as blank initially for institute field (coz they will be filled by the JavaScript once the user selects a City), but upon submitting the data, I get a "Select a valid choice" validation error. Any ideas how to tackle such problem?

但是学院的数量超过了3000所。因此,我希望最初为institute字段保留空白选项(因为一旦用户选择了一个城市,它们将由JavaScript填充),但是在提交数据时,我将得到一个“选择有效选项”验证错误。有什么办法来解决这个问题吗?

Note: When I do NOT specify choices (for institute field) the web page size is 300KB and when I specify them the size of web page is 1.1MB

注意:当我不指定选项(对于institute字段)时,web页面大小为300KB,当我指定选项时,web页面大小为1.1MB

3 个解决方案

#1


1  

Apparently, I had to override the Choice widget and change its render method to display no options. Since choices argument is still provided, the validation error does not occur and I can freely use javascript to render institutes by the city selected.

显然,我必须重写Choice小部件并更改其呈现方法以显示无选项。由于仍然提供了choice参数,因此不会出现验证错误,我可以*地使用javascript来呈现所选城市的研究所。

#2


0  

You should ensure that the Institute values that get filled up in the select box should also have the primary key as the html id attribute.

您应该确保在选择框中填充的研究所值也应该具有作为html id属性的主键。

#3


0  

I had the same issue, but overcomed it by setting widget.choices to []

我有同样的问题,但是通过设置小部件来克服它。选择[]

class StudentForm(forms.ModelForm):
    city = forms.ModelChoiceField(City)
    institute = forms.ModelChoiceField(queryset=Institute.objects.all())

def __init__(self, *args, **kwargs):
    super(StudentForm, self).__init__(*args, **kwargs)
    self.fields['institute'].widget.choices = []

#1


1  

Apparently, I had to override the Choice widget and change its render method to display no options. Since choices argument is still provided, the validation error does not occur and I can freely use javascript to render institutes by the city selected.

显然,我必须重写Choice小部件并更改其呈现方法以显示无选项。由于仍然提供了choice参数,因此不会出现验证错误,我可以*地使用javascript来呈现所选城市的研究所。

#2


0  

You should ensure that the Institute values that get filled up in the select box should also have the primary key as the html id attribute.

您应该确保在选择框中填充的研究所值也应该具有作为html id属性的主键。

#3


0  

I had the same issue, but overcomed it by setting widget.choices to []

我有同样的问题,但是通过设置小部件来克服它。选择[]

class StudentForm(forms.ModelForm):
    city = forms.ModelChoiceField(City)
    institute = forms.ModelChoiceField(queryset=Institute.objects.all())

def __init__(self, *args, **kwargs):
    super(StudentForm, self).__init__(*args, **kwargs)
    self.fields['institute'].widget.choices = []