Django - Custom Model Method - How to specify datatype so Admin formats it properly? -
example:
class mymodel(models.model): field1=models.charfield(..) field2=models.datetimefield() def today(self): return self.field2 when @ in admin site, field2 formatted differently today field.
how can tell admin site treat today it's treating field2? i.e., tell django admin 'today' models.datetimefield?
here it's showing:
field2 today april 5, 2011, 9:10 a.m. 2011-04-11 08:47:27
that's really weird behaviour. @ total guess, may have django settings; datetime_format (and related) settings. framework introspection on fields, , if of datetime type, rendered according aforementioned settings.
introspection on methods wouldn't make sense in majority of cases, understand behaviour if case.
try modifying settings accordingly (provide different datetime formats), , see if fields change , method remains same.
edit:
looking @ django.contrib.databrowse.datastructures, there section of code like:
if isinstance(self.field, models.datetimefield): objs = capfirst(formats.date_format(self.raw_value, 'datetime_format')) i'd imagine similar thing happening within admin app, though can't find exact reference @ moment.
to achieve want, you'll need format datetime appropriately:
def today(self): django.conf import settings return self.field2.strftime(settings.datetime_format) or, using @cata's comment:
def today(self): django.utils.formats import localize return localize(self.field2)
Comments
Post a Comment