How to not include an empty string in the icontains condition in model filter?
I'm currently developing an application using Django
.
If I use the filter
of the model
and set the target of icontains
to the empty string
″
as shown below, all the data is gotten.
queryset = MyModel.objects.filter(my_field__icontains='').all()
But I don't want to include it in the data if the icontains
target is an empty string
.
How can I enable it only if it does not include the empty string
and contains other values?
Python: 3.7.5
Django: 3.2.
Solution 1:
You check the target:
if target:
queryset = MyModel.objects.filter(my_field__icontains=target)
else:
queryset = MyModel.objects.none()