开发者

query with django orm

I need select rows with django orm. I need equivalent of such query

select * from order where (user_from = u and f1 not is null) or (user_to = u and f2 not is null)

I try to do this way:

Order.objects.filter(user_from = self).exclude(f1 = None)+Order.开发者_运维技巧objects.filter(user_to = self).exclude(f2 = None)

But no union in orm.. how can be such task done by orm? (i see one solution to add some fields in my model, but it intresting to solve it without fields adding)


Take a look at Q objects. You can execute something like:

Order.objects.filter(
    Q(user_from = u, f1__isnull = False) | Q(user_to = u, f2__isnull = False)
)

Warning: this is untested code. It would be a good idea to see the actual SQL query generated and verify that this is indeed what you need.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜