K
K
k0r0g2021-10-14 01:11:10
Django
k0r0g, 2021-10-14 01:11:10

How to formulate a django orm request?

Hello.
Let's say I have 2 tables: books and genres. They are related in a many-to-many relationship.

class Genre(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    genres = models.ManyToManyField(Genre)


There is a list of genres from filtering. For example, it would be How to select books that have filtering genres in their genres list? I tried something like this:
genres = ['Комедия', 'Триллер']

books = Book.objects.filter(genres__name__contains=genres) #тут пробовал и in

It didn't work. How to formulate a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2021-10-14
@k0r0g

genres__name__in=genres

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question