2024-04-25 18:03:10 发布
网友
我有一个过滤器查询,根据搜索条件不同
有时是:
Thing.object.filter(name__startswith=word).distinct('id')
有时:
Thing.object.filter(city__startswith=word)
所以.filter(之后的一切都可以改变。有没有办法动态设置?你知道吗
.filter(
.filter()的参数,是的。只要编一本字典。你知道吗
.filter()
D = {'city__startswith': word} things = Thing.object.filter(**D)
对.distinct()的调用,不需要。您需要使用特殊情况。你知道吗
.distinct()
如果“city”和“name”是条件选择器的值,则可以执行以下操作:
queryset = Thing.object.filter(**{'%s__startswith' % criteria: word}) if criteria_requires_distinct: queryset = queryset.distinct()
.filter()
的参数,是的。只要编一本字典。你知道吗对
.distinct()
的调用,不需要。您需要使用特殊情况。你知道吗如果“city”和“name”是条件选择器的值,则可以执行以下操作:
相关问题 更多 >
编程相关推荐