开发者

django orm, custom method on top of a queryset

开发者 https://www.devze.com 2023-03-31 14:23 出处:网络
lets suppose that some code in my view looks like this: bar = Bar.objects.get(pk=1) foos = bar.foo_set.filter(VERY_LONG_COMPLEX_FILTERING_LOGIC)

lets suppose that some code in my view looks like this:

bar = Bar.objects.get(pk=1)
foos = bar.foo_set.filter(VERY_LONG_COMPLEX_FILTERING_LOGIC)

I would obviously want to clean the filter method a bit, by creating a custom method that does the same thing, like so:

bar = Bar.objects.get(pk=1)
foos = bar.foo_set.complexfilter()

Moving the custom method somewhere to a class would be perfect, but so far I couldn't find any mention 开发者_C百科of anything like that in the docs. Any suggestions?

Thank you in advance.


Placing it in bar's method seems good.

0

精彩评论

暂无评论...
验证码 换一张
取 消