So my django book is back at university and I'm struggling to work this one out.
I've subclassed django.forms.widgets.MultiWidget
like so:
class DateSelectorWidget(widgets.MultiWidget):
def __init__(self, attrs=None, dt=None, mode=0):
if dt is not None:
self.datepos = dt
else:
self.datepos = date.today()
# bits of python to create days, months, years
# example below, the rest snipped for neatness.
years = [(year, year) for year in year_digits]
_widgets = (
widgets.Select(attrs=attrs, choices=days),
widgets.Select(attrs=attrs, choices=months),
widgets.Select(attrs=attrs, choices=years),
)
super(DateSelectorWidget, self).__init__(_widgets, attrs)
def decompress(self, value):
if value:
return [value.day, value.month, value.year]
return [None, None, None]
def format_output(self, rendered_widgets):
return u''.join(rendered_widgets)
Which gives me a nifty looking date selection field like so:
My queston is very simple really. When I submit said form to its handling method (which uses a process like this:
forminstance = ModelNameForm(request.POST, instance=modelinstance)
if forminstance.is_valid():
forminstance.save()
This fails, because Django doesn't know how to take my multi-widget and convert it back to the underlying field type, which is set in models.py
to DateField()
, clearly.
Now, the comments around the MultiWidget in the django source give me this useful hint:
You'll probably want to use this class with MultiValueField.
But the thing is - I probably don't. I want to keep my DateField()
because it is very useful and there is no point duplicating it. What I need to do then is somehow convert these multiple fields back into a single valid datestring (yyyy-mm-dd
) for insertion into the database.
My question is then:
How? What is the best way to achieve this?
See Question&Answers more detail:
os