Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported lookup 'level' for DateTimeField or join on the field not permitted

this is my view.py

@list_route(methods=["post"])
    def created_in_range(self, request):
        response = {}
        data = request.POST
        start = dateutil.parser.parse(data['start'])
        end = dateutil.parser.parse(data['end'])
        page_no = data['page_no']
        tweets = Tweet.get_created_in_range(start, end, int(page_no))
        serializer = TweetSerializer(tweets, many= True)
        response["data"] = serializer.data
        return Response(response, status= status.HTTP_200_OK)

this is my class method of models.py

@classmethod
    def get_created_in_range(cls, start, end, page_no):
        """
        Returns all the tweets between start and end.
        """
        tweets = cls.objects.filter(created_at__level__gte = start, created_at__level__lt=end )
        paginator = Paginator(tweets, 5)
        return paginator.page(page_no)

this is the error i get

Internal Server Error: /api/twitter/created_in_range/
Traceback (most recent call last):
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/rest_framework/viewsets.py", line 95, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/rest_framework/views.py", line 494, in dispatch
    response = self.handle_exception(exc)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/rest_framework/views.py", line 454, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/rest_framework/views.py", line 491, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/kethan/Desktop/twitter_env/twitter_app/api/views.py", line 67, in created_in_range
    tweets = Tweet.get_created_in_range(start, end, int(page_no))
  File "/home/kethan/Desktop/twitter_env/twitter_app/api/models.py", line 143, in get_created_in_range
    tweets = cls.objects.filter(created_at__level__gte = start, created_at__level__lt=end )
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/db/models/query.py", line 836, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/db/models/query.py", line 854, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1253, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1277, in _add_q
    split_subq=split_subq,
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1215, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1069, in build_lookup
    lhs = self.try_transform(lhs, name)
  File "/home/kethan/Desktop/twitter_env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1115, in try_transform
    (name, lhs.output_field.__class__.__name__))
django.core.exceptions.FieldError: Unsupported lookup 'level' for DateTimeField or join on the field not permitted.
[25/Feb/2018 02:19:25] "POST /api/twitter/created_in_range/ HTTP/1.1" 500 19151

this above is the response in the server i dont know why there's such a big deal with date time fields

can someone help me resolving the issue, i am fighting for this for a while now i dont understand where i am going wrong please help solving this....

like image 330
kethan bravo Avatar asked Feb 24 '18 20:02

kethan bravo


1 Answers

For the tweets in 2 date range, you just need the code below and level is not required.

tweets = cls.objects.filter(created_at__gte = start, created_at__lte=end )

Tweets created date should be greater than start date and lesser than the end date.

like image 104
Astik Anand Avatar answered Sep 30 '22 00:09

Astik Anand