【发布时间】:2017-04-26 11:59:42
【问题描述】:
我知道这是一个已知主题,但请听我说,我目前正在过滤query_params,我认为是这样的:
filter_date = self.request.query_params.get('filter_date', None)
if filter_date is not None:
queryset = queryset.filter(next_action_date__gte=filter_date)
return queryset
我正在通过next_action_date 显示我的联系人,现在我有这个自定义数据结构,我需要将它添加到这个过滤器中,以便它显示我的联系人从最重要到不重要,我已经尝试添加它到过滤器,但我没有得到任何数据,所以我怎样才能适当地完成这项工作,有人可以告诉我吗?
这是我的 get_queryset:
def get_queryset(self):
queryset = LeadContact.objects.none()
user = self.request.user
if user.has_perm('cms_sales.can_view_full_lead_contact_list'):
queryset = LeadContact.objects.all()
elif user.has_perm('cms_sales.can_view_lead_contact'):
queryset = LeadContact.objects.filter(account_handler=user)
filter_date = self.request.query_params.get('filter_date', None)
# This is the custom ordering data structure
virgin_data = LeadContact.objects.filter(status=LeadContactConstants.STATUS_PRISTINE)
contacted_data = LeadContact.objects.filter(status=LeadContactConstants.STATUS_CONTACTED)
qualified_data = LeadContact.objects.filter(status=LeadContactConstants.STATUS_QUALIFIED)
client_data = LeadContact.objects.filter(status=LeadContactConstants.STATUS_CLIENT)
# This needs to be added to the filter so it will properly order
# contacts
order_data = list(client_data) + list(qualified_data) + list(contacted_data) + list(virgin_data)
if filter_date is not None:
queryset = queryset.filter(next_action_date__gte=filter_date)
return queryset
【问题讨论】:
-
结果必须是 QuerySet 吗?可以排序,正常的对象列表吗?
-
@MateuszKnapczyk 好吧,我需要通过
next_action_date为我的联系人提供鞋子并正确订购它们,我愿意接受建议,你可以告诉我,你需要更多数据吗?
标签: django django-rest-framework django-queryset