与datefield - django分开显示日月和年的不同输入

时间:2015-01-16 04:13:37

标签: django django-models django-forms django-templates

在模板中,我想显示每个字段(日,月,年)与AboutForm分开,而不仅仅是一个输入,如下所示:

enter image description here

models.py:

class About(models.Model):
    user = models.OneToOneField(User)
    dob = models.DateField(null=True, blank=True)
    present_address = models.CharField(max_length=150, null=True, blank=True)
    permanent_address = models.CharField(max_length=150, null=True, blank=True)
    description = models.TextField(null=True, blank=True)

forms.py:

class AboutForm(forms.ModelForm):
    class Meta:
        model = About
        exclude = ('user',)

1 个答案:

答案 0 :(得分:3)

您可以使用SelectDateWidget小部件。

from django.forms import extras

...

class AboutForm(forms.ModelForm):
    class Meta:
        model = About
        exclude = ('user',)
        widgets = {
            'dob': extras.SelectDateWidget(years=range(1900, 2015))
        }

感谢Likeon指出这是内置于Django的。