#13560 closed (fixed)
SplitDateTimeField(localize=True) fails as value gets converted to string
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | 1.2 |
Severity: | Keywords: | localization, SplitDateTimeField, regression | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I tried the new localization-features in Django 1.2, activating localozation for all my fields in the admin using (why isn't this done by default anyway?):
class FooAdmin(admin.ModelAdmin): def get_form(self, *args, **kwargs): form_class = super(SSDAdmin, self).get_form(*args, **kwargs) for f in form_class.base_fields: form_class.base_fields[f].localize = True return form_class
After doing so my admin fails badly (meaning: Exception), I am not able to edit any models any more. After digging aroung for some time I found, that forms.BoundField converts the value to it's localized string before passing it to the field/widget (as_widget()). So SplitDateTimeWidget gets a string instead of the expected datetime-object and fails when trying to split date/time.
Simple test:
from django import forms from datetime import datetime class TestForm(forms.Form): datetime = forms.SplitDateTimeField(localize=True) form = TestForm(initial={'datetime': datetime.now()}) print form['datetime']
I think this could be fixed by overwriting the localize_value() method of SplitDateTimeField to always return the vanilla data. I'll append a patch doing so, but I am not sure if this is enough/appropriate.
I could think of some more dependancies MultiValueField's doesn't meet yet. For example I think field.localized should be set for every field in MultiValueField.fields, according to the value used in MultiValueField.localized.
Attachments (3)
Change History (11)
Changed 14 years ago by
Attachment: | localized_splitdatetimefield.patch added |
---|
comment:1 Changed 14 years ago by
comment:2 Changed 14 years ago by
Component: | Uncategorized → Forms |
---|---|
Triage Stage: | Unreviewed → Accepted |
Changed 14 years ago by
Attachment: | 13560.1.diff added |
---|
Updated form localization to also happen in the widgets for render time. Also fixes a bunch of bugs introduced in r12029.
comment:3 Changed 14 years ago by
Has patch: | set |
---|
comment:4 follow-up: 5 Changed 14 years ago by
@jezdez - I've just added a slightly revised version of the patch that rationalizes the localize_input and _format_values functions.
comment:5 Changed 14 years ago by
Triage Stage: | Accepted → Ready for checkin |
---|
Replying to russellm:
@jezdez - I've just added a slightly revised version of the patch that rationalizes the localize_input and _format_values functions.
Very cool, +1
comment:6 Changed 14 years ago by
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:7 follow-up: 8 Changed 14 years ago by
I understand it may be to late to fix this for 1.2.1, but the patch seem to badly broke the usability of SplitDateTimeField, when using date format localisation.
I'm using revision 13299 with USE_L10N set to True, and language set to Polish. When I try to edit something in admin, the date in SplitDateTimeField is formated as "YYYY-MM-DD" (ie. not localized), but after clicking "Today", or anything else in calendar, the format switches to localized "DD.MM.YYYY". The sudden format switch is really confusing. After saving, it switches again to "YYYY-MM-DD".
comment:8 Changed 14 years ago by
Replying to ludwik:
I'm sorry, I was confused. The issue is not connected to the patch (it still exists, thought. I think the "localize" option is just applied inconsistently, and JavaScript function always uses localized versions, while rest of the Django admin respects the "localize" option).
perhaps it would be best to move localization into the widget at all, because l10n should not change the state of the internal values (inside field or boundfield), but only it's representation in the frontend (widget).