这是我的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)
这是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)
这是我得到的错误
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
以上是服务器中的响应 我不知道为什么日期时间字段有这么大的优惠
有人可以帮助我解决这个问题吗,我现在正在争取这个问题 我不明白我哪里错了 请帮助解决这个问题....答案 0 :(得分:2)
对于2个日期范围内的tweets
,您只需要以下代码,并且不需要level
。
tweets = cls.objects.filter(created_at__gte = start, created_at__lte=end )
推文创建日期应大于开始日期且小于结束日期。