1

I have a listTextField in my modal called technology. I want to filter objects based on an array, if every array value is present in the technology field.

tech = ['a','b',....] #dynamic list
mentors_list = Mentor.objects.filter(
            **{"technology__contains" : value for value in tech}) #this doesn't work

Mentor class has (among other fields) :

class Mentor(Meta):
    technology = ListTextField(
        base_field=models.CharField(max_length=20),
        size=10, max_length=(10 * 11))

Basically i want all objects in mentor_list whose technology field must contain all the values from tech array ( but may contain some extra values ).

1 Answer 1

3

From the documentation You can chain Q object with & to check multiple entries contains in the ListTextField.

from functools import reduce 

technologies = ['a','b'] # your dynamic list

queries = [Q(technology__contains=technology) for technology in technologies]

query = reduce(lambda x, y: x & y, queries)

mentors_list = Mentor.objects.filter(query) 

Ref: How to dynamically compose an OR query filter in Django?

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to help you. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.