Reputation: 498
I have a movie database with has following models. I store the director details in Actor model itself. That is I create an actor and then link him as director. Through this I can get the director who is also an actor. Now my problem is to list all the directors. I am not able write the query. I get ReverseManyRelatedObjectsDescriptor most of the time.
class Actor(models.Model):
actorid = models.AutoField(primary_key=True)
name = models.CharField(max_length=250)
sex = models.CharField(max_length=1)
def director_list(self):
directors = self.filter(actor__in=Movie.director).distinct()
**#ReverseManyRelatedObjectsDescriptor error**
return directors
class Movie(models.Model):
movieid = models.AutoField(primary_key=True)
title = models.CharField(max_length=250)
actor = models.ManyToManyField(Actor, related_name="actor")
director= models.ManyToManyField(Actor, related_name="director")
Please help, thanks in advance.
Upvotes: 0
Views: 96
Reputation: 5193
If I understood well your question, your need those actors who are directors (without a specific movie). So what I'd done is create a class method of Actor, like this:
class Actor(models.Model):
actorid = models.AutoField(primary_key=True)
name = models.CharField(max_length=250)
sex = models.CharField(max_length=1)
@classmethod
def director_list(cls):
directors_available = set (
d.actorid
for movie in Movie.objects.filter(director__is_null = False)
for d in movie.director.all()
)
actors_directors = Actor.objects.filter(actorid__in = list(directors_available))
return actors_directors
And now you can get all directors that are actors like this:
director_list = Actor.director_list()
Upvotes: 1