Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude by object in queryset

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?

like image 683
linkyndy Avatar asked Jun 22 '26 15:06

linkyndy


1 Answers

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])
like image 186
linkyndy Avatar answered Jun 24 '26 06:06

linkyndy