What side should a django 'many-to-many' relationship reside on

Viewed 1403

Lets say I have a recipe website with two basic models, 'User' and 'Recipe'

class User(models.Model):
    username= models.CharField()
    email = models.CharField()

class Recipe(models.Model):
    name = models.CharField()
    description = models.CharField()

I would like to add the functionality so that users can 'favorite' a recipe.

In this case, I need to use a many-to-many relationship. My question is, how do I decide which model to add the relationship to?

For example, each user could have a list of 'favorite' recipes:

class User(models.Model):
    favorites = models.ManyToManyField(Recipe)

Alternatively, each recipe could have a list of users who Favorited the recipe:

class Recipe(models.Model):
    user_favorites = models.ManyToManyField(User)

What is considered the best practice? Is either one better for query performance?

1 Answers
Related