【问题标题】:chain filter query params链式过滤器查询参数
【发布时间】: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


【解决方案1】:

如果您需要 QuerySet 作为结果,请尝试使用 queryset = queryset.filter(next_action_date__gte=filter_date).order_by('status'),但它可能不是您想要的顺序。

但如果您只需要一个过滤后的排序列表(而不是 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)
    if filter_date is not None:
        queryset = queryset.filter(next_action_date__gte=filter_date)

    # Filter our queryset already filtered by date (if given)
    virgin_data = list(queryset.filter(status=LeadContactConstants.STATUS_PRISTINE))
    contacted_data = list(queryset.filter(status=LeadContactConstants.STATUS_CONTACTED))
    qualified_data = list(queryset.filter(status=LeadContactConstants.STATUS_QUALIFIED))
    client_data = list(queryset.filter(status=LeadContactConstants.STATUS_CLIENT))
    # Just add them together
    order_data = client_data + qualified_data + contacted_data + virgin_data    

    return order_data

编辑

我找到了更好的解决方案here

order = [
    LeadContactConstants.STATUS_CLIENT,
    LeadContactConstants.STATUS_QUALIFIED,
    LeadContactConstants.STATUS_CONTACTED,
    LeadContactConstants.STATUS_PRISTINE
]
order_data = sorted(queryset, key = lambda p: order.index(p.status))

【讨论】:

  • 很好,这更好,谢谢,让我玩一下,你提供的第一个解决方案就像一个魅力
猜你喜欢
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 2014-04-06
  • 1970-01-01
  • 2013-10-03
相关资源
最近更新 更多