Antoine
Antoine

Reputation: 587

How to perform model.objects.get(**kwargs) with case insensitive

I would like to get an instance of one of my Django models by using dictionary in an insensitive case way. For the moment I'm using:

my_model.objects.get(**dictionary)

But I don't get the instance if there is a difference of lower / upper case between what there is in my dictionary and the fields of the instance.

Do you think there is a clean way to perform this operation ?

Upvotes: 0

Views: 592

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123350

Django takes the WHERE filter to load the object entirely from the keys of the dictionary you provide. It is up to you to provide field lookups; you can use the iexact field lookup type, but you do need to use this on textual fields.

So if you wanted to match a specific string field, case insensitively, add __iexact to the key in dictionary; e.g. {'foo': 'bar'} becomes {'foo__iexact': 'bar'}.

Upvotes: 4

Related Questions