Question

I would like to exclude some objects from my queryset and was wondering how can I pass a list of objects to Django's exclude().

Here's what I have so far:

pages = [page1, page2] # page1 and page2 are Page objects
Page.objects.filter(site=site).exclude(pages)

I can do a exclude(pk__in=[p.pk for p in pages]) but it doesn't feel natural. How can I specify a list of objects to be excluded from the above queryset?

Was it helpful?

Solution

It seems that my approach is one of the best, so far. Here's what I've ended up with:

pages = [page1, page2] # page1 and page2 are Page objects
Page.objects.filter(site=site).exclude(pk__in=[p.pk for p in pages])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top