[Answer]-Access many-to-many related to the same model

1๐Ÿ‘

โœ…

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()
๐Ÿ‘คxecgr

Leave a comment