我有这个查询
Location.objects.filter(locations_rate__rate=search_rate).distinct('id')
如何将distinct()
设置为**kwargs
?
我试过了:
s_kwargs = {}
s_kwargs['locations_rate__rate'] = request.GET.get('rate')
但不知道在哪里设置distinct('id')
?我还有许多其他过滤器,例如locations_rate__rate
,我会问if locations_rate__rate in s_kwargs
然后设置这个distinct()
吗?
答案 0 :(得分:1)
**kwargs
只是一本字典。
你可以这样做:
def myView(request, *args, **kwargs):
#Code here.
distinct = kwargs.pop('distinct') #the distinct parameter(s) {'distinct': 'rate'}
#verification and validation of distict
Location.objects.filter(locations_rate__rate=search_rate).distinct(distinct)
#I assumed only 1 distinct argument
...
如果你想要动态参数,
params = {'rate': distinct} #where distinct is distinct = kwargs.pop('distinct')
things = Thing.object.filter(**params)