Search code examples
pythondjangodjango-modelsmany-to-many

Retrieve the target object of many to many relationship


I'm creating a many to many through relationship to create a follow/follower relationship

User -> Relationship -> User

I want to retrieve all the User objects following a particular User but currently all I can do is retrieve a queryset of Relationship objects.


Solution

  • Answer is:

    User.objects.filter(to_relationships__user=self)
                                        ^^
    

    the double underscore allows you to query across relationships