Code

Opened 4 years ago

Closed 4 years ago

Last modified 4 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: UI/UX:

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@…> 4 years ago.
13560.1.diff (9.6 KB) - added by jezdez 4 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 russellm 4 years ago.
Minor cleanup of v1 patch

Download all attachments as: .zip

Change History (11)

Changed 4 years ago by David Danier <david.danier@…>

comment:1 Changed 4 years ago by just an idea

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 4 years ago by jezdez

  • Component changed from Uncategorized to Forms
  • Triage Stage changed from Unreviewed to Accepted

Changed 4 years ago by jezdez

Updated form localization to also happen in the widgets for render time. Also fixes a bunch of bugs introduced in r12029.

comment:3 Changed 4 years ago by jezdez

  • Has patch set

Changed 4 years ago by russellm

Minor cleanup of v1 patch

comment:4 follow-up: Changed 4 years ago by russellm

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

comment:5 in reply to: ↑ 4 Changed 4 years ago by jezdez

  • Triage Stage changed from Accepted to 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 4 years ago by jezdez

  • Resolution set to fixed
  • Status changed from new to closed

(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 follow-up: Changed 4 years ago by ludwik

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 in reply to: ↑ 7 Changed 4 years ago by ludwik

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.