I have a model field as below.
GENDER_IN_CHOISES = (
(MALE, _('Male')),
(FEMALE, _('Female')),
(UNDEFINED, _('Undefined')),
)
gender = models.CharField(max_length=2, choices=GENDER_IN_CHOISES, default=UNDEFINED)
I am making queries with object filtering however below does not satisfy my needs.
Users.objects.filter(gender__contains='male')
Do you know a way to make queries to choice fields?
if MALE
and FEMALE
are defined as 'ML'
and 'FL'
, then the contains='male'
or the case-insensitive version icontains='male'
queries would be useless because the database fields contain 'ML' and 'FL'
Your query would be more along the lines of:
Users.objects.filter(gender__in=('ML', 'FL'))
But you should reuse your constants to keep the code DRY (Don't Repeat Yourself)
Users.objects.filter(gender__in=(MALE, FEMALE))
Or, exclude the undefined genders
Users.objects.exclude(gender=UNDEFINED)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With