0

How is it possible to create a relationship like this:

Imagine a restaurant. Among their datatables is one for recipes and one for ingredients. And there is another one, which contains categories of food such as dinner, lunch, breakfast, dessert, soup. Let’s name it cat_food.

The cook wants to sort recipes and ingredients by using cat_food. For example there is a ‘ManyToOne’ relationship from recipes to cat_food among all the other columns. So one recipe has one category and one category has several recipes.

But the cook also wants to sort the ingredients that way. Here we would have a ‘ManyToMany’ relationship, while we can use one ingredient in a dessert recipe just like in a dinner or soup recipe as well.

One solution could be to create a cat_food table for recipe and ingredients for its own. But how is it possible to create the cat_food table, which stores either recipes or ingredients?(depends on what is given)

My thought is to use an intermediate model and set both ‘foreignKeys’ with the field options ‘blank=True’ and ‘null=True’. But I am not satisfied with that. Is it possible that it can cause some database problems on the long term? Is there a more elegant way to handle such a situation?

pillow_willow
  • 99
  • 3
  • 11

2 Answers2

0

I'm not sure if this is what you're looking for, but perhaps you could create boolean values for each category for your two different tables?

For example, if your chef had the item 'strawberries' and your different categories were dinner, lunch, breakfast, dessert, and soup. Then you can check off the categories this food could go into. For example: breakfast == True, dessert == True, soup == False. and on for the rest of the categories.

If you're doing both, then you can make it so that in your template you have both recipes and ingredients, and have buttons or a dropdown to filter the results.

  • I like the idea, but... I want to make it as less redundant as possible. What I mean is if there is a new item for cat_food then I just want to place it there to have it as new option in recipes or ingredients. Ok here my example is a bit on the edge, because it is hard to create a new food category... Let's say the new one is astronout-dinner... Now when I add a new recipe or ingredient I want to have a dropdown menü with all the possible cat_food items. And I want to use as less columns as possible. – pillow_willow Apr 01 '16 at 17:52
  • That makes sense, maybe you can create a third content type? I think you can import a content type that you create separately. A manyToMany relationship might also work, I've used those before and they're fairly useful. Try this link to see if it'll work- https://docs.djangoproject.com/en/1.9/topics/db/examples/many_to_many/ – overstreetce Apr 01 '16 at 17:58
  • My problem is that two tables communicate with the same one, which is cat_food. By this I have a column for recipes and for ingredients in cat_food. When one is given the other one is empty. I could handle this via "blank=True" and "null=True" I guess. But I am not quite sure about troubleshooting later on. – pillow_willow Apr 01 '16 at 18:10
  • You could also automatically give ingredients in a recipe the same categories as the recipes they're in? – overstreetce Apr 01 '16 at 18:21
  • Unfortunately not, because all the ingredients should be given by default. I need to fill them in before people can choose them. Ok my cooking example is a really bad one I see... So it is like this. I am the cook who serves the database with all the ingredients and people making recipes. The cat_food is for query’s in a later stage and while the database is growing I am adding categories (cat_food) to the ingredients, when they have been used in a new category (like I said it is actually a bad example with cooking and food) – pillow_willow Apr 06 '16 at 10:28
0

IMHO, the standard relationships, foreign key and many-to-many as you said, seem to be the best; because you can manage each item (recipe, ingredient, category) with suitable fields and django supports reference for their linkage very well so making queries considering their relations becomes quite easy.

In addition, if you want a customized M2M table with extra fields, you can build yourself using through attibute.

Leonard2
  • 894
  • 8
  • 21
  • So you would say it is a good way to do it like this. Create one cat_food with recipes and ingredients, while both of them have the field options "blank=True" and "null=True". So it can be either a recipe item or an ingredients. Yes I am thinking about to use an immediate table via through. I guess this is helpful in this case – pillow_willow Apr 06 '16 at 10:29