A. S.
A. S.

Reputation: 89

Narrowing down a QuerySet in Django by using difference() and/or exclude()

I'm trying to narrow down a QuerySet in Django. I'm trying to do this by excluding (or taking the difference of) another QuerySet.

I've already tried using difference() and exclude() but both of them do not work for some reason. Besides, I’m confused by the error messages I get.

Here’s the code:

ipdb> all_products
<QuerySet [<Product: P1>, <Product:  P2>, <Product: P3>, <Product: P4>, <Product: P5>, <Product: P6>]

ipdb> added_products
<QuerySet [<Product: P1>, <Product:  P2>, <Product: P3>]

ipdb> all_products.exclude(added_products)
*** ValueError: too many values to unpack (expected 2)

ipdb> all_products.difference(added_products)
*** django.db.utils.DatabaseError: ORDER BY not allowed in subqueries of compound statements.

Upvotes: 1

Views: 612

Answers (1)

user8060120
user8060120

Reputation:

by doc the exclude you should given lookup parameters, try to use pk__in

all_products.exclude(pk__in=added_products)

more details querysets in

Upvotes: 1

Related Questions