I have a localized Django App, localisation works well and the configuration is ok…
For forms needs, I use __unicode__
method for model to render ModelChoiceFields, but how do I format localized date in the unicode return?
In this method I have no access to current timezone, how to display my TimeSpanList correctly to my users? Currently, it displays UTC. I tryed django.template.defaultfilters.date
and Simon Charette's django.utils.formats.localize
which didn't helped as they probably lacked contextual data…
class TimeSpan(models.Model):
start = models.DateTimeField(_("start"))
end = models.DateTimeField(_("end"))
def __unicode__(self):
return u"from UTC:{0} to UTC:{1}".format(self.start, self.end)
class TimeSpanChooserForm(forms.Form):
time_span = forms.ModelChoiceField(
label=_("time span"), queryset=TimeSpan.objects.all())
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
super(TimeSpanChooserForm, self).__init__(*args, **kwargs)
How to know current locale without current request object to localize those datetimes? (If there is a way)
note: to me __unicode__
seems like the only way to display entries in ModelChoiceField.
note 2: to me, Yuji 'Tomita' Tomita comment is the best current answer but it lacks a usable exemple…