year/month/day/hour/minute/second filters in django-rest-framework -


i've defined django-rest filter this:

created_date = django_filters.datetimefilter(     lookup_type=["year", "month", "day", "hour", "minute", "second", "lt", "lte", "gt", "gte"]) 

this url proper query: created_date < 2015-05-01:

http://localhost:8000/path/objects/?created_date__lt=2015-05-01

the same thing works gt for: created_date > 2015-05-01:

http://localhost:8000/path/objects/?created_date__gt=2015-05-01

this url proper query: created_date > 2015-05-01

however, when try query on year:

http://localhost:8000/path/objects/?created_date__year=2015

i

typeerror: int() argument must string or number, not 'datetime.datetime' 

i tried

http://localhost:8000/path/objects/?created_date__year=2015-05-01

and same error. idea how year/month/day/hour/minute/second filters working?

datetimefilter filter converts input value datetime, created_date__year compared datetime , wrong. has compared int.

solution 1:

add filter same field:

created_date_part = django_filters.numberfilter(     name='created_date',     lookup_type=["year", "month", "day", "hour", "minute", "second"]) 

however need create name field.

solution 2:

write custom filter inherit charfilter , validation itself.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -