Answer the question
In order to leave comments, you need to log in
How to check the manytomany field for the presence of an object in it?
There is a user model, and a product
class User(AbstractBaseUser):
purchased = models.ManyToManyField('Movie', verbose_name='Купленные товары', blank=True)
class Movie(models.Model):
title = models.CharField(verbose_name='Название', max_length=100)
Answer the question
In order to leave comments, you need to log in
As an option
movie1 = Movie.objects.get(pk=1)
movie2 = Movie.objects.get(pk=2)
User.objects.filter(pk=1, purchased__in=[movie1, movie2]).exists()
movie = Movie.objects.get(pk=1)
user = User.objects.get(pk=1)
user.purchased.filter(pk=movie.pk).exists()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question