Scott Benson
Scott Benson

Reputation: 83

how can I delete all non-matching query results in django?

Say I have a list of values

list =['value1', 'value2', 'value3', ...]

Is there a way I can run a query against a whole Db and delete any entries that don't match?

The opposite of the below almost:

models.objects.filter(value__in=list).delete()

Upvotes: 2

Views: 646

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477318

You can work with .exclude(…) [Django-doc], so:

Model.objects.exclude(value__in=list).delete()

Upvotes: 2

Related Questions