Answer the question
In order to leave comments, you need to log in
How to get the field by which the django object was found?
For example, there is a model:
class TestModel(models.Model):
title = models.CharField()
desc = models.CharField()
other_field = models.CharField()
result = TestModel.objects.filter(Q(title__icontains='test') | Q(desc__icontains='test') | Q(other_field__icontains='test') )
Answer the question
In order to leave comments, you need to log in
The end goal is a little unclear. If you just look at the value of the object field - for some reason you can’t - then you can add what you want to the annotated fields:
.annotate(
title_contains=Case(
When(
Q(title__icontains='test'),
then=V('True')
),
default=(V(False)),
output_field=BooleanField()
),
desc_contains=Case(
When(
Q(desc__icontains='test'),
then=V('True')
),
default=(V(False)),
output_field=BooleanField()
),
...
for obj in result:
print(obj.title_contains) # триггернулось ли по title
print(obj.desc_contains) # триггернулось ли по desc
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question