简体   繁体   中英

Django - choice set, but no drop down in form

I would like to have a choice set in my model for template and validation.

However, in the model.form for that model I want to have just an integer field.

How can I change the widget in order to just get a InputField?

I tried to change the widget to forms.Model, but that did not seem to work. I get a error:

'IntegerField' object has no attribute 'attrs'

forms.py:

class KombiPublikationForm(forms.ModelForm):

    class Meta:
        model = KombiPublikation
        #fields = []
        exclude = ['pub_sprache']
        widgets = {
            'monat': forms.IntegerField(), # does not work
        }

model.py:

MONTH = (
    (1, 'Januar'),
    (2, 'Februar'),
    (3, 'März'),
    (4, 'April'),
    (5, 'Mai'),
    (6, 'Juni'),
    (7, 'Juli'),
    (8, 'August'),
    (9, 'September'),
    (10, 'Oktober'),
    (11, 'November'),
    (12, 'Dezember'),
)

class KombiPublikation(models.Model):
    [...]
    monat = models.IntegerField(choices=MONTH)

Thanks!

我找到了解决方案-NumberInput()是正确的小部件。

from django.utils.dates import MONTHS

choices=MONTHS.items()  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM