简体   繁体   English

Django-选择集,但没有下拉列表

[英]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. 但是,在该模型的model.form中,我只希望有一个整数字段。

How can I change the widget in order to just get a InputField? 如何更改小部件以仅获取InputField?

I tried to change the widget to forms.Model, but that did not seem to work. 我试图将小部件更改为forms.Model,但这似乎不起作用。 I get a error: 我收到一个错误:

'IntegerField' object has no attribute 'attrs'

forms.py: forms.py:

class KombiPublikationForm(forms.ModelForm):

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

model.py: 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()  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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