SelectDatewidget does not shows the correct value

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

SelectDatewidget does not shows the correct value

Rocio Figueroa

Hello
Within a form, a field date:

UserForm = forms.models.form_for_instance(user)
UserForm.base_fields [ 'record'].widget = SelectDateWidget()

Does not shows the correct value, always shows the value of January 1,
2008, showing how to make the correct value

Thanks

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: SelectDatewidget does not shows the correct value

theskyiscrape

SelectDateWidget cannot handle datetime fields. I subclassed the
widget:

class DatetimeSelectDateWidget(SelectDateWidget):
    """Overrides render to provide string value expected by
SelectDateWidget.
    """
    def render(self, name, value, attrs=None):
        # The SelectDateWidget in django cannot handle datetime value,
incredibly.
        if value is not None:
        value = str(value)
        return super(FixedSelectDateWidget, self).render(name, value,
attrs)



On Apr 1, 9:22 am, "Rocio Figueroa" <[hidden email]> wrote:

> Hello
> Within a form, a field date:
>
> UserForm = forms.models.form_for_instance(user)
> UserForm.base_fields [ 'record'].widget = SelectDateWidget()
>
> Does not shows the correct value, always shows the value of January 1,
> 2008, showing how to make the correct value
>
> Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: SelectDatewidget does not shows the correct value

Gary Davis-4
In reply to this post by Rocio Figueroa

I had this same problem.  I think it is still a problem even after the
fixes marked #5027 and #5917.
I'm a Django newbie, so please forgive me if I'm just confused or if I
have missed any future fixes that address this problem.
Gary Davis

Here's a suggested patch to the SelectDateWidget: (to add lines 2-6,
the ones including, and between, the comments)

    def value_from_datadict(self, data, name):
        #### look first for the data in the 'normal' format
        full_value = data.get(name, None)
        if full_value:
            return full_value
        #### else: look for the individual components
        y, m, d = data.get(self.year_field % name),
data.get(self.month_field % name), data.get(self.day_field % name)
        if y and m and d:
            return '%s-%s-%s' % (y, m, d)
        return None

Here's my test suite:

class OriginalRecipe(forms.Form):
    name=forms.CharField()
    birthday=forms.DateField()

class ExtraCrispy(Form):
    name=forms.CharField()
 
birthday=forms.DateField(widget=SelectDateWidget(years=range(2008,2010)))

test_data = {'name':'Teddy Tester',
'birthday':datetime.date(2008,9,30)}

print "HTML from the 'stock' date widget"
print OriginalRecipe(data=test_data)

print
print "HTML from the SelectDateWidget"
print ExtraCrispy(data=test_data)

#  without the patch above, the ExtraCrispy output has no date values
selected;
#  with the patch, the year, month, and day are selected.


On Apr 1, 9:22 am, "Rocio Figueroa" <[hidden email]> wrote:

> Hello
> Within a form, a field date:
>
> UserForm = forms.models.form_for_instance(user)
> UserForm.base_fields [ 'record'].widget =SelectDateWidget()
>
> Does not shows the correct value, always shows the value of January 1,
> 2008, showing how to make the correct value
>
> Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---