M
M
mezigar2022-04-09 08:46:47
Django
mezigar, 2022-04-09 08:46:47

How to link tables in django so that you can do multiple select in multiple select?

Available models:

from django.db import models
# from django.contrib.postgres.fields import ArrayField


class Cocktail(models.Model):
    title = models.CharField(max_length=50, db_index=True, verbose_name='Название')
    # ingredients = ArrayField(, blank=True)
    ingredients = models.ManyToManyField('Ingredient', verbose_name='Ингредиенты')
    # portions = models.ManyToManyField('Portion', 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)
    portion = models.ManyToManyField('Portion', null=True, blank=True)

    def __str__(self):
        return f"{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, null=True, blank=True, verbose_name='Порция')

    def __str__(self):
        return self.portion

    class Meta:
        verbose_name_plural = 'Порции'
        verbose_name = 'Порция'

How to make it so that in the admin panel, when creating a cocktail from ingredients, you can select a portion of each ingredient?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question