NickP
NickP

Reputation: 1414

Django Model Form Integer as Drop Down

I have a simple Model form in my project as follows:

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.

Is there a way to overwrite an integer input as such?

Upvotes: 0

Views: 381

Answers (1)

user8193706
user8193706

Reputation: 2425

You can use a ChoiceField as

class InviteForm(forms.ModelForm):
    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)
    class Meta:
        model = Invite
        fields = ['max_guests', 'rsvp_guests', 'rsvp_attendance']
        

Upvotes: 1

Related Questions