I have two models Movie and Actor
class Actor(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)
name = models.CharField(name="name", max_length=2000, blank=True, null=True)
class Movie(models.Model):
id = models.UUIDField(default=uuid.uuid4, primary_key=True, editable=False)
name = models.CharField(name="name", max_length=2000, blank=True, null=True)
actors = models.ManyToManyField(Actor, related_name='movie_actor')
Now I would like to let the users to add their favourite actors like
class MyUser(AbstractUser):
actors = models.ManyToManyField(Actor, related_name='user_actor')
Now, I would like to make the response for each movie so that the movie can tell that this movie has one/more of their favourite actors or not?
CodePudding user response:
To tell if a movie has one or more favorite actors of a given user:
has_favorite_actors = movie.actors.filter(user_actor=user).exists()
CodePudding user response:
You can simply filter the Actor objects by the two parameters to get all actors in your_movie
that are favored by your_user
.
Actor.objects.filter(movie_actor = your_movie, user_actor = your_user)
If you just want to check if favored actors exist, add the exists()
method.
Actor.objects.filter(movie_actor = your_movie, user_actor = your_user).exists()