Answer the question
In order to leave comments, you need to log in
How to make proper model hierarchy in Django?
I have cocktail, ingredient and serving models. I don’t understand how to make it so that when choosing an ingredient, you need to specify its portion. How should models be organized so that everything works correctly? At the moment the models look like this:
from django.db import models
class Cocktail(models.Model):
title = models.CharField(max_length=50, db_index=True, verbose_name='Название')
ingredients = models.ManyToManyField('Ingredient', verbose_name='Ингредиент')
# Stars or rating = Many to One
def __str__(self):
return f"{self.title}: id = {self.id} "
class Meta:
verbose_name_plural = 'Коктейли'
verbose_name = 'Коктейль'
class Ingredient(models.Model):
# portion = models.CharField(max_length=6, choices=PORTIONS, blank=True)
title = models.CharField(max_length=30, db_index=True, verbose_name='Название')
portion = models.ForeignKey('Portion', null=True, blank=True, on_delete=models.SET_NULL)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = 'Ингредиенты'
verbose_name = 'Ингредиент'
class Portion(models.Model):
PORTIONS = (
('50 ml', 'одна часть'),
('75 ml', 'полторы части'),
('100 ml', 'две части'),
('150 ml', 'три части'),
)
portion = models.CharField(max_length=6, choices=PORTIONS, blank=True, verbose_name='Порция')
def __str__(self):
return self.portion
class Meta:
verbose_name_plural = 'Порции'
verbose_name = 'Порция'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question