[Django]-Can I filter a django model with a python list?

3👍

Renaming l for readability:

names = ['Bob','Dave','Jane']

Person.objects.[exclude][1](Name__[in][2]=names)

UPDATE 1: Answer to the second question (in your ‘EDIT’ paragraph):

present = Person.objects.values_list('Name', flat=True)
absent = set(names) - set(present)   
# or, if you prefer named functions to the set operator '-'
absent = set(names).difference(present) 

Yes, the “right hand side” of difference (but not ‘-‘) accepts any iterable (I had to look it up to confirm).

👤msw

4👍

This should work:

Person.objects.exclude(name__in=['Bob','Dave','Jane'])

Leave a comment