#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)
by , 15 years ago
Attachment: | localized_splitdatetimefield.patch added |
---|
comment:1 by , 15 years ago
comment:2 by , 15 years ago
Component: | Uncategorized → Forms |
---|---|
Triage Stage: | Unreviewed → Accepted |
by , 15 years ago
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 by , 15 years ago
Has patch: | set |
---|
follow-up: 5 comment:4 by , 14 years ago
@jezdez - I've just added a slightly revised version of the patch that rationalizes the localize_input and _format_values functions.
comment:5 by , 14 years ago
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 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
follow-up: 8 comment:7 by , 14 years ago
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 by , 14 years ago
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).