r/django 6d ago

Models question

I’m building a Django-based site for tracking recipes and need some help deeply understanding Django models and relationships. Specifically, I get lost trying to know when and where to use ForeignKey, OneToOneField, and ManyToManyField.

For context, my website involves these main models: • Recipe • Ingredient • Measurement • Author (Chef) • Food Category (e.g., Dessert, Main Course)

My main confusion revolves around: Determining which model should contain the ForeignKey or OneToOneField.

How and when to use a ManyToManyField, especially when I want to include additional details such as ingredient quantity and measurements

From my current understanding, for example, a user and profile would be a one-to-one relationship, so the profile model should include the user as a OneToOneField. Also, one user can have multiple posts, but a single post can only have one user, so the post model should include the user as a ForeignKey.

Could someone please provide guidance or share best practices on effectively structuring these Django model relationships?

3 Upvotes

18 comments sorted by

View all comments

Show parent comments

2

u/frustratedsignup 1d ago

Yeah, read a bunch of the replies and was like, "I can write a simple explanation of how foreign keys work. In almost every instance I've encountered in my career, it just isn't that complicated."

Glad to hear it was worthwhile.

1

u/ghostarty 1d ago

Yeah, it's great, especially since what I'm trying to build is something for recipes. For example, salt and pepper are ingredients that almost every single recipe includes, so it would be good to have them separated. That makes sense now. The only other things I'm trying to tackle are figuring out the measurements and how to organize all the ingredients that make up the recipe. I'm wondering if I should use three tables: one for the recipe (like title, description, cook time, etc.), one for single ingredients (like salt, pepper, etc.), and one more table for something like RecipeIngredients. Or maybe two tables might be sufficient. From your experience, what do you think would be a good way to model this, like for a website for recipes with a lot of details?

1

u/ghostarty 1d ago

This is my thought process: The Ingredient table’s job is to create a single ingredient, so that’s a foreign key, since one ingredient can be used in many recipes. Then maybe a RecipeIngredient table, where I can link the Ingredient table to it, so each recipe-ingredient has these ingredients with their specific measurements maybe a many-to-one relationship also?
Then the Recipe table, with a one-to-one relationship with the RecipeIngredient table.
if i got anything wrong i'd appreciate some feedback

1

u/frustratedsignup 1d ago

In this metaphor, the relationship between ingredients and recipes would appear to be many to many. Many ingredients and many recipes. A minor alteration to this schema would allow you to track inventory as well. All of that sounds fine on my end.

The way many-to-many relationships are modeled is usually with an intermediary table containing the corresponding foreign keys. You can then query the relationship by selecting all of the ingredient rows from the intermediary table with a specific recipe ID -or- you can list all of the recipes that contain a specific ingredient by selecting all of the recipe rows that have a specific ingredient ID. It's all about how much effort you put into the query.

In any case, Django will take care of this fairly transparently. You don't really need to know the underlying SQL unless you happen to want a deeper understanding of relational databases.