1

我有这个查询

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()可能吗?

4

1 回答 1

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)
于 2013-05-20T13:54:40.333 回答