views:

24

answers:

1

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)

+1  A: 

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.

Manoj Govindan
It get exception:Join on field 'to_url_placed_update' not permitted. Did you misspell 'is_null' for the lookup type?I see it for the first time for using __is_null=False You shure this feature present in django orm?
Evg
My mistake. `is_null` should have been `isnull`. Upadting answer.
Manoj Govindan
this works thanx.
Evg