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

Error when trying to filter by a date range of a datetime field #96

Open
swiharta opened this issue Apr 14, 2020 · 1 comment
Open

Error when trying to filter by a date range of a datetime field #96

swiharta opened this issue Apr 14, 2020 · 1 comment

Comments

@swiharta
Copy link

swiharta commented Apr 14, 2020

Hi, I use your app with Django Rest Framework, and it's been perfect for my needs. Thank you so much for maintaining it!

I ran into a little issue today. I have a model with a datetime field called start. I am able to filter a range of dates like this:

http://127.0.0.1:8000/api/userevent/?start__range=2020-03-29,2020-05-03

This works fine, except that I end up leaving out some events at the end of the range. In the above example, since I'm in the US Eastern timezone, events on 2020-05-03 that have a start time 4 or 5 hours before midnight (depending on daylight savings time) will be omitted from the result, due to datetimes being stored as UTC in the database.

This is the equivalent Django query:

UserEvent.objects.filter(start__range=(start, end))

Thankfully, Django is also able to filter QuerySets by a range of dates for datetime fields, as such:

UserEvent.objects.filter(start__date__range=(start, end))

This results in different SQL which will correctly include those edge events I mentioned.

However, I get an error when I try to do this using django-url-filter:

http://127.0.0.1:8000/api/userevent/?start__date__range=2020-03-29,2020-05-03

The traceback shows a problem that seems to start in url_filter\integrations\drf.py in filter_queryset(), line 161 return _filter.filter(), where _filter shows a RecursionError.

Below are the local variables captured by Django during the execution of that function:

Variable | Value
_filter | Error in formatting: RecursionError: maximum recursion depth exceeded in comparison
filter_class | <class 'abc.UserEventFilterSet'>
filter_model | <class 'radcal.models.UserEvent'>
model | <class 'radcal.models.UserEvent'>
queryset | <QuerySet [bunch of events..., '...(remaining elements truncated)...']>
request | <rest_framework.request.Request object at 0x0579B238>
self | <url_filter.integrations.drf.DjangoFilterBackend object at 0x0579B9A0>
view | <radcal.views.UserEventViewSet object at 0x05734D18>

This eventually errors out in django\forms\fields.py in to_python(), line 379 value = value.strip().

AttributeError: 'dict' object has no attribute 'strip'

Here are the local variables at that point:

Variable | Value
self | <django.forms.fields.DateTimeField object at 0x0579BFE8>
value | {'range': <LookupConfig start__date__range=>'2020-03-29,2020-05-03'>}

I am aware that there are other ways to address my problem, but I would like to know if this represents a fixable issue vs an accepted limitation when using django-url-filter. I've included the full stack trace below. I'm using Django 2.2.12, DRF 3.11.0, and django-url-filter 0.3.15. Please let me know if I can provide any more information that might be helpful.

Traceback:

File "C:\Websites\envs\radfile\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Websites\envs\radfile\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Websites\envs\radfile\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Websites\envs\radfile\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "C:\Websites\envs\radfile\lib\site-packages\rest_framework\viewsets.py" in view
  114.             return self.dispatch(request, *args, **kwargs)

File "C:\Websites\envs\radfile\lib\site-packages\rest_framework\views.py" in dispatch
  505.             response = self.handle_exception(exc)

File "C:\Websites\envs\radfile\lib\site-packages\rest_framework\views.py" in handle_exception
  465.             self.raise_uncaught_exception(exc)

File "C:\Websites\envs\radfile\lib\site-packages\rest_framework\views.py" in raise_uncaught_exception
  476.         raise exc

File "C:\Websites\envs\radfile\lib\site-packages\rest_framework\views.py" in dispatch
  502.             response = handler(request, *args, **kwargs)

File "C:\Websites\radres\radcal\views.py" in list
  350.       queryset = self.filter_queryset(self.get_queryset())

File "C:\Websites\envs\radfile\lib\site-packages\rest_framework\generics.py" in filter_queryset
  150.             queryset = backend().filter_queryset(self.request, queryset, self)

File "C:\Websites\envs\radfile\lib\site-packages\url_filter\integrations\drf.py" in filter_queryset
  161.                 return _filter.filter()

File "C:\Websites\envs\radfile\lib\site-packages\url_filter\filtersets\base.py" in filter
  298.             specs = self.get_specs()

File "C:\Websites\envs\radfile\lib\site-packages\url_filter\filtersets\base.py" in get_specs
  334.                 specs.append(self.get_spec(data))

File "C:\Websites\envs\radfile\lib\site-packages\url_filter\filtersets\base.py" in get_spec
  390.         return self.filters[name].get_spec(value)

File "C:\Websites\envs\radfile\lib\site-packages\url_filter\filters.py" in get_spec
  398.         value = self.clean_value(value, lookup)

File "C:\Websites\envs\radfile\lib\site-packages\url_filter\filters.py" in clean_value
  352.         return form_field.clean(value)

File "C:\Websites\envs\radfile\lib\site-packages\django\forms\fields.py" in clean
  148.         value = self.to_python(value)

File "C:\Websites\envs\radfile\lib\site-packages\django\forms\fields.py" in to_python
  462.         result = super().to_python(value)

File "C:\Websites\envs\radfile\lib\site-packages\django\forms\fields.py" in to_python
  379.         value = value.strip()

Exception Type: AttributeError at /api2/userevent/
Exception Value: 'dict' object has no attribute 'strip'
@realnot
Copy link

realnot commented Oct 22, 2021

I have the same problem, anyone know how to fix this?

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

No branches or pull requests

2 participants