使用 SelectDateWidget 的 Django 表单字段

2022-01-18 00:00:00 python django widget admin forms

问题描述

我已经从 Django 安装了最新的 SVN 分支,其中包括新的表单.我正在尝试使用 django.forms.extras.widgets 中的 SelectDateWidget,但该字段显示为普通的 DateInput 小部件.

I've installed the latest SVN branch from Django which includes the new forms. I'm trying to use the SelectDateWidget from django.forms.extras.widgets but the field is showing up as a normal DateInput widget.

这是我的应用程序中的 forms.py:

Here is the forms.py from my application:

from django import forms
from jacob_forms.models import Client

class ClientForm(forms.ModelForm):
    DOB = forms.DateField(widget=forms.extras.widgets.SelectDateWidget)

    class Meta:
            model = Client

我做错了什么?检查 forms/extras/widgets.py 我看到 SelectDateWidget 类存在.

What am I doing wrong? Checking the forms/extras/widgets.py I see the SelectDateWidget class exists.


解决方案

真正的问题是不能以这种方式引用 SelectDateWidget.更改代码以不同方式引用它解决了我的问题:

The real problem was that SelectDateWidget can't be referenced this way. Changing the code to reference it differently solved my problem:

from django.forms import extras
...
    DOB = forms.DateField(widget=extras.SelectDateWidget)

这似乎是一个限制,您不能从导入的包中引用 package.package.Class.该解决方案导入 extras,因此引用只是 package.Class.

This seems to be a limitation that you can't reference package.package.Class from an imported package. The solution imports extras so the reference is just package.Class.

相关文章