我正在使用django-filter在django中显示我的结果
但是我必须根据模型中的属性过滤一些结果我还要使用列表理解
def extra_available_list(request):
EXTEND = "rent_base.html"
extra_list_gen = ExtraFilter(request.GET, queryset=Extra.objects.all().order_by('is_parking'))
extra_list = [obj for obj in extra_list_gen if ((obj.lease)==0)]
paginator = Paginator(extra_list, 10) # Show 25 contacts per page
page = request.GET.get('page')
try:
extra_page = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
extra_page = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
extra_page = paginator.page(paginator.num_pages)
return render(request,'unit/extra_list.html', {'extra': extra_list,'page_extra':extra_page})
如何将列表推导的输出合并到我的django过滤器中?
更新
我尝试过建议的解决方案
extra_list_gen = Extra.objects.all() extra_list_ids = [obj for obj in extra_list_gen if((obj.lease)== 0)] extra_list = ExtraFilter(request.GET,queryset = Extra.objects.filter(id__in = extra_list_ids).order_by(' is_parking'))
但是我收到了错误
int()参数必须是字符串或数字,而不是'额外'
extra_list = ExtraFilter(request.GET, queryset=Extra.objects.filter(id__in=extra_list_ids).order_by('is_parking'))
答案 0 :(得分:2)
因为你需要一个查询集,你可以做这样的事情
# First get the ids that you want, saving the id to the list
extra_objects_ids = [obj.id for obj in Extra.objects.all() if ((obj.lease)==0)]
# Now filter using id__in and then order
extra_queryset = Extra.objects.filter(id__in=extra_objects_ids).order_by('is_parking')