Django rest framework 基于url中kwarg的django过滤器查询集
我试图根据url中的kwarg筛选queryset。 我的url类似于:Django rest framework 基于url中kwarg的django过滤器查询集,django-rest-framework,django-urls,django-polymorphic,Django Rest Framework,Django Urls,Django Polymorphic,我试图根据url中的kwarg筛选queryset。 我的url类似于: http://localhost:8000/notification/all/?type="workflow" 我想筛选url中是否存在类型参数 class NotificationList(generics.ListAPIView): model = Notification serializer_class = NotificationSerializer def get_queryset(
http://localhost:8000/notification/all/?type="workflow"
我想筛选url中是否存在类型参数
class NotificationList(generics.ListAPIView):
model = Notification
serializer_class = NotificationSerializer
def get_queryset(self):
queryset = Notification.objects.filter(created_for=self.request.user)[:int(settings.NOTIFICATION_PAGE_SIZE)]
type_param = self.request.query_params.get('type')
last = self.request.query_params.get('last')
print("TYPE PARAm", type_param)
if str(type_param)=="workflow":
print("TRUEEEEEEEEEEEEEeeeee")
queryset = Notification.objects.filter(created_for=self.request.user).instance_of(WorkflowNotification)
if last:
last_notification_created_on = Notification.objects.get(id=last)
queryset = queryset.filter(created_on__lt=last_notification_created_on)
return queryset
目前,即使我将url中的类型指定为工作流,它也没有进入if条件。可能有什么问题?请应用此选项
从url中删除
如果类型不是工作流,只需引发错误
if type == 'workflow':
print("TRUEEEEEEEEEEEEEeeeee")
queryset = ....
else:
from rest_framework.exceptions import NotFound
raise NotFound(detail="no notifications", code=404)
我想返回一个错误,如:否notifications@MohitHarshan,请看一看。