[Django]-Django 1.8: When items are related via two separate relationships, how can I specify which relationship to use?

2👍

Django doesn’t even allow you to define two relationships between the same models unless you define related_name. So you use that attribute.

class Interest(models.Model):
    user_selected = models.ManyToManyField(
         User, through="Selected", related_name="selected_interests")
    user_recommended = models.ManyToManyField(
         User, through="Recommended", related_name="recommended_interests")


my_user.selected_interests.all()  # Interests where the user is in `user_selected`
my_user.recommended_interests.all()  # Interests where the user is in `user_recommended`

Leave a comment