-1

I'm trying to fetch users list based on their language(from ArrayField). A single user can have multiple languages. When i passed single language{'Hindi'}, it fetches all the records except user with multiple languages, but if passed parameters {'Hindi','bangla','kannada'} then it gives me a specific record,how can i fetch the users list with hindi and other than hindi as well . I have tried with .all also but it didn't worked for me. Any help would be appreciated.

enter image description here

enter image description here models.py

# DESCRIPTION: This function gets the drivers list by lang.
    @classmethod
    def get_driver_by_lang(cls, driver_lang):
        try:
            driver_details = cls.objects.filter(language = driver_lang)
            data = serializers.serialize("json", driver_details)
            data = json.loads(data)
            return data
        except :
            return False
2
  • try the suggested answer and the comment. Commented Jul 22, 2019 at 6:20
  • also you probably have the wrong database design. searching for specific array elements can be a sign of database misdesign. check this answer Commented Jul 22, 2019 at 6:20

1 Answer 1

2

How about this:

driver_details = cls.objects.filter(language__contains = driver_lang)
Sign up to request clarification or add additional context in comments.

3 Comments

won't it be cls.objects.filter(language__contains=[driver_lang])?
without '[]' it is working. let me check again with [].
with the [] it gives an error, but without it , its working. Thanks @mhd

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.