Yogi
Yogi

Reputation: 1579

Filtering object model django with list of values

I am building a django project and I have a list of ids [23,44,12,67] and I have a model named testmodel

 tests = testmodel.objects.all()

But I want to remove/filter(I don't want to remove from database, just filter) the objects which has the ids in my list. Any help how I can achieve this in a simple way using django ?

Upvotes: 1

Views: 77

Answers (3)

kt14
kt14

Reputation: 846

try this:

test = testmodel.objects.exclude(pk__in=[23,44,12,67])

You can find more information here

Upvotes: 2

Gocht
Gocht

Reputation: 10256

You need the .exclude() ORM method:

testmodel.objects.exclude(id__in=[23,44,12,67])

Upvotes: 2

Shang Wang
Shang Wang

Reputation: 25559

tests = testmodel.objects.exclude(id__in=[23, 44, 12, 67])

Upvotes: 2

Related Questions