Excellent that it was reported, but bad that it's not considered a 'bug' -- Any suggestions on how to use date_hierarchy (and probably others) with my field?
On Aug 10, 8:04 am, Alex Gaynor <alex.gay...@gmail.com> wrote: > On Mon, Aug 10, 2009 at 8:45 AM, BenW<benwil...@gmail.com> wrote: > > > Hello, > > > I'm working with a legacy database that stores datetimes as unsigned > > ints. Rather than do the conversion with properties on the model I've > > written a custom Field 'UnixDateTimeField': > > > class UnixDateTimeField(models.DateTimeField): > > > __metaclass__ = models.SubfieldBase > > > def get_internal_type(self): > > return 'PositiveIntegerField' > > > def to_python(self, value): > > if value is None or isinstance(value, datetime): > > return value > > if isinstance(value, date): > > return datetime(value.year, value.month, value.day) > > return datetime.fromtimestamp( float(value) ) > > > def get_db_prep_value(self, value): > > return int( time.mktime( value.timetuple() ) ) > > > def value_to_string(self, obj): > > value = self._get_val_from_obj(obj) > > return self.to_python(value).strftime('%Y-%m-%d %H:%M:%S') > > > class MyModel(models.Model): > > time = UnixDateTimeField() > > > # This Works: > >>> MyModel.objects.filter(time__lte=datetime.now()) > > [<MyModel>, <MyModel>, etc..] > > > # This works: > >>> MyModel.objects.all()[0].time > > datetime.datetime(2009, 8, 10, 6, 40, 7) > > > # Doesn't work: > >>> MyModel.objects.all().values('time') > > [{'time': 1249911607L}, {'time': 1249911607L}, {'time': > > 1249911607L}, ...] > > > The same thing happens in the Admin when I specify date_hierarchy in > > my ModelAdmin a one of these fields. Why are the standard accessor > > methods (namely 'to_python()') not being called here? How can I make > > the custom Field more robust? > > > Thank you, > > > Ben > > This looks like ticket #9619:http://code.djangoproject.com/ticket/9619 > > Alex > > -- > "I disapprove of what you say, but I will defend to the death your > right to say it." -- Voltaire > "The people's good is the highest law." -- Cicero > "Code can always be simpler than you think, but never as simple as you > want" -- Me --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---