Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#13560 closed (fixed)

SplitDateTimeField(localize=True) fails as value gets converted to string

Reported by: David Danier <david.danier@…> 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)

localized_splitdatetimefield.patch (398 bytes ) - added by David Danier <david.danier@…> 15 years ago.
13560.1.diff (9.6 KB ) - added by Jannis Leidel 15 years ago.
Updated form localization to also happen in the widgets for render time. Also fixes a bunch of bugs introduced in r12029.
13560.2.diff (10.2 KB ) - added by Russell Keith-Magee 15 years ago.
Minor cleanup of v1 patch

Download all attachments as: .zip

Change History (11)

by David Danier <david.danier@…>, 15 years ago

comment:1 by just an idea, 15 years ago

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).

comment:2 by Jannis Leidel, 15 years ago

Component: UncategorizedForms
Triage Stage: UnreviewedAccepted

by Jannis Leidel, 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 Jannis Leidel, 15 years ago

Has patch: set

by Russell Keith-Magee, 15 years ago

Attachment: 13560.2.diff added

Minor cleanup of v1 patch

comment:4 by Russell Keith-Magee, 15 years ago

@jezdez - I've just added a slightly revised version of the patch that rationalizes the localize_input and _format_values functions.

in reply to:  4 comment:5 by Jannis Leidel, 15 years ago

Triage Stage: AcceptedReady 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 Jannis Leidel, 15 years ago

Resolution: fixed
Status: newclosed

(In [13296]) Fixed #13560 -- Fixed localization of widgets.

Particularly this fixes the SplitDateTimeField and the AdminDateWidget by localizating the widget's value in its render method instead of the form field. Thanks to David Danier for the report and Russell for help with the patch.

comment:7 by Ludwik Trammer, 15 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".

in reply to:  7 comment:8 by Ludwik Trammer, 15 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).

Note: See TracTickets for help on using tickets.
Back to Top