Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for queries with datetime #2

Open
gertjanol opened this issue Dec 19, 2016 · 6 comments
Open

Add support for queries with datetime #2

gertjanol opened this issue Dec 19, 2016 · 6 comments

Comments

@gertjanol
Copy link

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.

@myyang
Copy link
Owner

myyang commented Dec 19, 2016

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
Copy link
Owner

myyang commented Dec 19, 2016

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

@gertjanol
Copy link
Author

gertjanol commented Dec 19, 2016

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.

@myyang
Copy link
Owner

myyang commented Dec 19, 2016

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

@prashantidealittechno
Copy link

When you have time to update for new verison?

@myyang
Copy link
Owner

myyang commented Oct 7, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants