2

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
  • Can you post the source code for Order model? Commented Aug 23, 2010 at 14:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
My mistake. is_null should have been isnull. Upadting answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.