L
L
Leonid2020-02-13 11:58:47
Django
Leonid, 2020-02-13 11:58:47

How to add dependent selection in Django admin?

There is a class Product Options

class Options(models.Model):
    """Опции для товара - минералы, пищевая ценность, витамины"""

    TITLE_CHOICES = (
        ("food", "Пищевая ценность"),
        ("vitamins", "Витамины"),
        ("minerals", "Минералы"),
    )
    INFO_CHOICES = (
        ("carb", "Углеводы"),
        ("protein", "Белки"),
        ("fats", "Жиры"),
        ("kal", "кКал"),
    )

    title = models.CharField(
        max_length=10, default="food", choices=TITLE_CHOICES, verbose_name="Тип опций"
    )
    info = models.CharField(
        max_length=10, default="carb", choices=INFO_CHOICES, verbose_name="Значение опции"
    )
    quantity = models.PositiveSmallIntegerField(
        verbose_name="Количество в товаре", default=0
    )


How to achieve that in the admin panel, when selecting the title field TITLE_CHOICES in the info field, it shows only certain values? For example:
If you select Nutritional value in the title, the info field would select only Carbohydrates and Proteins
If you select Vitamins, the info field would select only Fats
The lists are non-overlapping. Of course, you can enter just symbolic values, but then it will inevitably double

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Andreev, 2020-02-13
@LostAlly

The question has been changed, now it sounds like a friend.)
How can the Junge understand carbohydrates are vitamins or minerals.
Well, in any case, you need a separate table where there is data on dependencies.
You need to normalize your database or even revise the logic.
If you normalize the database, you will get two reference tables where you will connect energy and feces, and the third with data where you will already enter data. (Unless, of course, I understand correctly what you want at all.)
PS For me, you first need to speculate the database normally, and only then ask questions to janga) good luck.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question