I have MyModel
:
class MyModel(models.Model):
ELEMENTS_CHOICES = Choices(
(1, 'element1'),
(2, 'element2'),
(3, 'element3'),
(4, 'element4'),
(10, 'element10'),
(12, 'element12'),
)
elements = MultiSelectField(
_('elements'),
choices=ELEMENTS_CHOICES,
blank=True,
)
I have also some MyModel
objects with elements
values like below:
obj1.elements = ['1', '2', '3', '10', '12']
obj2.elements = ['1', '3', '10', '12']
obj3.elements = ['1', '3', '10']
obj4.elements = ['2']
How do I filter through this field values?
The filter I tried to use is:
search_id = 2
MyModel.objects.filter(elements_icontains=search_id)
It was ok before the two-digit numbers appeared in the MyModel
field choices.
Now it returns obj1
, obj2
(because 12
is on the list, and it contains 2 as the part of it) and obj4
.
I would like to search just for values with 2
number and get just obj1
and obj4
as results. Currently, the results are objects that contains 2
and 12
.
What is the right way to filter this field?