英文:
Customize options inside the ModelChoiceField Django forms
问题
我有一个类似这样的Django表单:
class AddUserGroupFrom(forms.ModelForm):
class Meta:
model = UsersGroups
fields = (
'tag',
)
widgets = {
'tag': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '工作,家庭,等等..'}),
}
def __init__(self, *args, **kwargs):
super(AddUserGroupFrom, self).__init__(*args, **kwargs)
self.fields['city'] = forms.ModelChoiceField(
queryset=Group.objects.values_list('city').all().distinct(),
widget=forms.Select(attrs={
'class': 'form-select form-select-sm mb-3',
'aria-label': '.form-select-sm example',
}),
label='选择城市名称',
empty_label='选择城市',
)
self.fields['group'] = forms.ModelChoiceField(
queryset=Group.objects.values_list('group_number').all(),
widget=forms.Select(attrs={
'class': 'form-select form-select-sm mb-3',
'aria-label': '.form-select-sm example',
}),
label='选择组编号',
empty_label='选择组',
)
问题是在我的选择字段中,选项以元组的形式显示。模板如下所示:
<form action="" method="post">
{% csrf_token %}
<div class="mb-3">
<label for="{{ add_group_form.tag.id_for_label }}" class="form-label">{{ add_group_form.tag.label }}</label>
{{ add_group_form.tag }}
</div>
<label for="{{ add_group_form.city.id_for_label }}">{{ add_group_form.city.label }}</label>
{{ add_group_form.city }}
<label for="{{ add_group_form.group.id_for_label }}">{{ add_group_form.group.label }}</label>
{{ add_group_form.group }}
<input type="submit" value="保存更改" class="btn btn-success">
</form>
如何删除选择字段内部的元组样式表示?
我尝试在模板中使用索引进行迭代:
{{ add_group_form.city[0] }}
但会抛出错误:
无法解析余额: 'add_group_form.city[0]' 的 '[0]'
英文:
I have a django form like this:
class AddUserGroupFrom(forms.ModelForm):
class Meta:
model = UsersGroups
fields = (
'tag',
)
widgets = {
'tag': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Work, home, etc..'}),
}
def __init__(self, *args, **kwargs):
super(AddUserGroupFrom, self).__init__(*args, **kwargs)
self.fields['city'] = forms.ModelChoiceField(
queryset=Group.objects.values_list('city').all().distinct(),
widget=forms.Select(attrs={
'class': 'form-select form-select-sm mb-3',
'aria-label': '.form-select-sm example',
}),
label='Choose city name',
empty_label='Select city',
)
self.fields['group'] = forms.ModelChoiceField(
queryset=Group.objects.values_list('group_number').all(),
widget=forms.Select(attrs={
'class': 'form-select form-select-sm mb-3',
'aria-label': '.form-select-sm example',
}),
label='Choose group number',
empty_label='Select group',
)
The problem that in my select field options displayed as tuples
Form representation
Templates look like this:
<form action="" method="post">
{% csrf_token %}
<div class="mb-3">
<label for="{{ add_group_form.tag.id_for_label }}" class="form-label">{{ add_group_form.tag.label }}</label>
{{ add_group_form.tag }}
</div>
<label for="{{ add_group_form.city.id_for_label }}">{{ add_group_form.city.label }}</label>
{{ add_group_form.city }}
<label for="{{ add_group_form.group.id_for_label }}">{{ add_group_form.group.label }}</label>
{{ add_group_form.group }}
<input type="submit" value="Save changes" class="btn btn-success">
</form>
How can I remove tuple-style representation in options inside the select field?
I tried to iterate within indexes in templates:
{{ add_group_form.city[0] }}
But it throws error:
> Could not parse the remainder: '[0]' from 'add_group_form.city[0]'
答案1
得分: 0
你可以创建一个包含城市数值的元组,并将其分配给表单字段的选择项。通常情况下,你不会在init
方法中定义表单字段。看起来city
不是一个模型字段,这就是为什么我将其更改为ChoiceField
。
class AddUserGroupFrom(forms.ModelForm):
city = forms.ChoiceField(
choices=[],
empty_label='选择城市',
label='选择城市名称',
widget=forms.Select(attrs={
'class': 'form-select form-select-sm mb-3',
'aria-label': '.form-select-sm example',
}),
)
group = forms.ModelChoiceField(
queryset=Group.objects.all(),
empty_label='选择群组',
label='选择群组编号',
widget=forms.Select(attrs={
'class': 'form-select form-select-sm mb-3',
'aria-label': '.form-select-sm example',
}),
)
class Meta:
model = UsersGroups
fields = ('tag',)
widgets = {
'tag': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '工作、家庭等..'}),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
city_list = Group.objects.values_list('city', flat=True)
city_choices = [(city, city) for city in city_list]
self.fields['city'].choices = city_choices
请注意,上述代码中的字符串已经翻译成中文。
英文:
You can create a tuple with the city values and assign it to the choices of the form field. Normally you do not define the form fields in the init method. It looks like city is not a model field. That's why I changed it to a ChocieField.
(code is not tested, change it if needed)
class AddUserGroupFrom(forms.ModelForm):
city = forms.ChoiceField(
choices=[],
empty_label='Select city',
label='Choose city name',
widget=forms.Select(attrs={
'class': 'form-select form-select-sm mb-3',
'aria-label': '.form-select-sm example',
}),
)
group = forms.ModelChoiceField(
queryset=Group.objects.all(),
empty_label='Select group',
label='Choose group number',
widget=forms.Select(attrs={
'class': 'form-select form-select-sm mb-3',
'aria-label': '.form-select-sm example',
}),
)
class Meta:
model = UsersGroups
fields = ('tag',)
widgets = {
'tag': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Work, home, etc..'}),
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
city_list = Group.objects.values_list('city', flat=True)
city_choices = [(city, city) for city in city_list]
self.fields['city'].choices = city_choices
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论