views:

1342

answers:

3

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.

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

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

+1  A: 

From the ticket re: the lack of documentation for SelectDateWidget here: Ticket #7437

It looks like you need to use it like this:

widget=forms.extras.widgets.SelectDateWidget()

Note the parentheses is the example.

bchang
Good suggestion, but you actually don't need to instantiate the Widget class when you pass it as the attribute, unless you have specific configuration parameters you want that widget's constructor to have. The django Form framework actually checks to see if the widget is a class or an instance, and will instantiate it for you if necessary.
Jarret Hardie
Unfortunately this didn't work, Jarret is correct.
timbonicus
A: 

Your code works fine for me as written. In a case like this, check for mismatches between the name of the field in the model and form (DOB versus dob is an easy typo to make), and that you've instantiated the right form in your view, and passed it to the template.

Jarret Hardie
Instantiating the form with a view solved the problem, or at least led me to other, more exciting problems. :) I had a misunderstand of how this worked.
timbonicus
A: 

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)

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.

timbonicus