Django query with variable number of filter arguments

左心房为你撑大大i 提交于 2021-02-07 18:04:22

问题


I have a Django query that fetches from MyModel based on certain conditions:

if beta:
    MyModel.object.filter(x=alpha, y=beta)
else:
    MyModel.object.filter(x=alpha)

Is it possible to eliminate the if beta: check and do it in a single line i.e. make the query filter on y only when beta is not None

Is this a good (Djangonic) way:

MyModel.object.filter(**{'x':alpha, 'b':beta} if beta else **{'x':alpha})

Or is it possible to do something like this (I know the following is wrong, but can it be fixed to give the correct meaning?):

MyModel.object.filter(Q('x'=alpha) & (beta && Q('y'=beta)))

回答1:


In such situations I'm using solution like below:

filter_kwargs = {'x': alpha}
if beta:
    filter_kwargs['y'] = beta

 MyModel.objects.filter(**filter_kwargs)

It is easy to expand when new conditions come in into the project requirements, but unfortunately its not a one-liner solution.




回答2:


In my opinion it isn't good solution. I have problem to read and understand the code.

You can also do something like this:

objects_ = MyModel.objects.filter(x=alpha, y=beta) if beta else MyModel.objects.filter(x=alpha)

return objects_

For me it's more clean.. but it's only my opinion.




回答3:


I would use

objects = MyObject.objects.filter(x=alpha)
if beta:
    # Additional filter because of ...
    objects = objects.filter(y=beta)

Your way is simply harder to read. Python should be easy to read. Note that this only works right for simple filters (no multi-valued relations), as buffer mentions. Otherwise, your original query looks best to me.




回答4:


One way to do this is to use Q objects, see this answer.

In your case :

query = Q(x=alpha)
if beta:
    query = query & Q(y=beta)
MyModel.object.filter(query)

Not shorter than other examples, but it could be cleaner if you were to add more variables to test.

HTH,



来源:https://stackoverflow.com/questions/23753158/django-query-with-variable-number-of-filter-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!