django-unixtimestampfield icon indicating copy to clipboard operation
django-unixtimestampfield copied to clipboard

Add support for queries with datetime

Open gertjanol opened this issue 8 years ago • 6 comments

Hi! Thanks for this module! I was hoping I could use this to do queries based on datetime objects, but that doesn't seem possible. Is this something you've left out on purpose? Would it be very hard to implement (I could help with it, but might need some pointers :))?

I'd expected the following to work:

from django.db import models
from unixtimestampfield.fields import UnixTimeStampField

class MyModel(models.Model):
     created = UnixTimeStampField(default=timezone.now)
>>> from datetime import datetime
>>> from my_models import MyModel
>>> m = MyModel.objects.create()
>>> m.created
datetime.datetime(2016, 12, 19, 13, 41, 8, 746901, tzinfo=<DstTzInfo 'Europe/Amsterdam' CET+1:00:00 STD>)
>>> MyModel.objects.filter(created__date=datetime.date(2016, 12, 19))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

It seems like this module could benefit from a symmetric API (datetime in, datetime out, while in the background floats are used for storage). Right now I need to worry about implementation details when using this module, like doing queries with floats. I hope you can shed some light on this.

gertjanol avatar Dec 19 '16 12:12 gertjanol

Hi, this TypeError seems raised by datetime.date method, not model field conversion.

All supported types (which are listed in readme example and test cases) would be converted float type while performing in database, cause I handle it with to_timestamp() and it's indeed and just a float field.

myyang avatar Dec 19 '16 13:12 myyang

I tried with ipython interpreter:

In [1]: from datetime import datetime

In [2]: datetime.date(2016, 12,13)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-e5cf39769324> in <module>()
----> 1 datetime.date(2016, 12,13)

TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

Hope above comment helps :P

myyang avatar Dec 19 '16 13:12 myyang

Sorry, made a mistake with my imports (usage of datetime.datetime.date instead of datetime.date). The example should read:

>>> from datetime import date
>>> from my_models import MyModel
>>> m = MyModel.objects.create()
>>> m.created
datetime.datetime(2016, 12, 19, 13, 41, 8, 746901, tzinfo=<DstTzInfo 'Europe/Amsterdam' CET+1:00:00 STD>)

The error now comes from Django:

>>> MyModel.objects.filter(created__date=date(2016, 12, 19))

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<redacted>/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "<redacted>/local/lib/python2.7/site-packages/django/db/models/query.py", line 790, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "<redacted>/local/lib/python2.7/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "<redacted>/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "<redacted>/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "<redacted>/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1203, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "<redacted>/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1097, in build_lookup
    lhs = self.try_transform(lhs, name, lookups)
  File "<redacted>/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1115, in try_transform
    (name, lhs.output_field.__class__.__name__))
FieldError: Unsupported lookup 'date' for UnixTimeStampField or join on the field not permitted.

The behaviour I'm aiming for (__date) only landed in Django 1.9. Is UnixTimeStampField supposed to handle these filter things correctly?

It appears as though filtering on any date thingy doesn't work:

>>> MyModel.objects.filter(created__year=2016)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
[..]
FieldError: Unsupported lookup 'year' for UnixTimeStampField or join on the field not permitted.

gertjanol avatar Dec 19 '16 14:12 gertjanol

If you could help, PR is always welcome. 👍

I finished supporting 1.8 (which is also LTS version) around 1 years ago. But, you know, people are lazy, maybe I'll improve compatibility "sometime" :P

myyang avatar Dec 19 '16 15:12 myyang

When you have time to update for new verison?

prashantidealittechno avatar Sep 06 '19 13:09 prashantidealittechno

Oops, sorry. I'm busy with my work and life. Anyone is able to help? 🆘

myyang avatar Oct 07 '19 02:10 myyang