문제

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?

도움이 되었습니까?

해결책

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])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top