Jonathan Livni
Jonathan Livni

Reputation: 107092

Django - How to transform a QuerySet to a Q object?

Is there a way to transform a QuerySet to a Q object in django?

My specific motivation: I would like to subtract one QuerySet (qs_A) from another QuerySet (qs_B). The only way I can think of is by using exclude() and a Q object equivalent of qs_A.

Example:

def my_function(qs_A, qs_B):
    # Here I need to transform qs_A to a Q object q_obj_A
    qs_new = qs.exclude(q_obj_A)
    return qs_new

Upvotes: 1

Views: 630

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You don't need a Q object. Just exclude the second queryset:

qs = qs_a.exclude(id__in=qs_B)

Upvotes: 5

Related Questions