我有一个URL,其中有一些查询参数,我已经对这个URL应用了分页。你知道吗
网址.py
url(r'^users/(?P<pk>[0-9]+)/workouts/get/$',
WorkoutList.as_view(serializer_class=WorkoutSerializer), name='list'),
视图.py
class WorkoutList(generics.ListCreateAPIView):
queryset = Workout.objects.all()
serializer_class = WorkoutSerializer
permission_classes = (UserPermissions,)
def get_queryset(self):
query_set = super(WorkoutList, self).get_queryset()
query_params = self.request.QUERY_PARAMS.dict()
try:
date = string_to_date_convertor(query_params['date'])
except KeyError:
print 'Exception'
return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
if 'date' in query_params:
query_set = Workout.objects.filter(created__contains=date, user_id = self.kwargs['pk'])
elif 'date' in query_params and 'exclude_app_install_time' in query_params:
query_set = Workout.objects.filter(created__contains=date, time_reg = query_params['exclude_app_install_time'])
return query_set
现在,我尝试除了块周围的日期,如果日期参数是没有做什么,只是返回4xx Http状态码。如果没有参数,只需返回4xx状态码。你知道吗
如果两个条件块都失败,则返回查询集并执行此查询集
queryset = Workout.objects.all() My Workout tables contain millions of entries and I don't want to return the whole table. That would be catastrophic.
所以我添加了另一个条件块
elif query_param is None:
query_set = None
简单地说,如果任何异常或条件块失败,那么只需返回4xx。你知道吗
当任何异常或条件块失败时,可以使用^{} 返回
EmptyQuerySet
。你知道吗相关问题 更多 >
编程相关推荐