[英]Django Model Form Integer as Drop Down
I have a simple Model form in my project as follows:我的项目中有一个简单的 Model 表格,如下所示:
class InviteForm(forms.ModelForm):
class Meta:
model = Invite
fields = ['max_guests', 'rsvp_guests', 'rsvp_attendance']
Max Guests is just an integer and appears for the user to type in. As the value is always small (<10), I would like this to be a dropdown instead with all values 0-max. Max Guest 只是一个 integer 并出现供用户输入。由于值总是很小(<10),我希望这是一个下拉列表,而不是所有值 0-max。
Is there a way to overwrite an integer input as such?有没有办法像这样覆盖 integer 输入?
You can use a ChoiceField as您可以使用ChoiceField作为
class InviteForm(forms.ModelForm):
class Meta:
model = Invite
fields = ['max_guests', 'rsvp_guests', 'rsvp_attendance']
MAX_GUESTS = (
('0', '0'),
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
('8', '8'),
('9', '9'),
('10', '10'),
)
max_guests = forms.ChoiceField(choices=MAX_GUESTS)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.