使用URL中的字符串进行Django动态查询集过滤

问题描述 投票:0回答:1

有什么方法可以动态过滤查询集,即我们从url中获得两个字符串值并搜索模型,其中第一个字符串是模型的attribute并获取包含特定字符串中第二个字符串的所有对象属性

django django-models django-filter
1个回答
0
投票
嗨,您只需要遵循此solution并使其适合您的情况,就可以了

Model.objects.values('attribute', 'id') # using '.values' [{'attribute': 'some attribute', 'id': 1 }, {'attribute': 'some 3', 'id': 2}, {'attribute': 'something', 'id': 3}, {'attribute': 'some some', 'id': 4}] # returns list of dictionaries

使用“第二个字符串”过滤或搜索词典列表,然后使用ID进行新查询。您可以修改此one

Model.objects.filter(id__in=object_ids)

© www.soinside.com 2019 - 2024. All rights reserved.