aman
aman

Reputation: 393

Django JSONField query on a list of values where at least 1 value is in the filter list

I have this model:

class Movie(models.Model):
    genres = models.JSONField(default=dict, null=False, blank=False)

The data inside this column contains a list of genres e.g.:

["biography", "comedy", "crime", "drama", "romance"]

When I try and execute the following code, I get empty:

wanted_genres = ['action', 'adventure', 'animation', 'biography', 'comedy', 'crime', 'documentary', 'drama', 'family']

Movie.objects.all().distinct().filter(genres__contains = wanted_genres)

I want to get results where at least 1 genre of wanted_genres is found in the column genres. I have tried a lot of filter techniques but I can't find a good array to array comparison method. Need help. Thanks.

Upvotes: 0

Views: 1285

Answers (1)

VikasDev
VikasDev

Reputation: 49

use contained_by instead of contains

Movie.objects.all().distinct().filter(genres__contained_by = wanted_genres)

For more information you can check official documentation

Upvotes: 0

Related Questions