D
D
delongeroman2021-03-05 16:40:15
Django
delongeroman, 2021-03-05 16:40:15

How to make a substitution in the field when entering data through the admin panel, having a relationship between several tables?

Not sure if I phrased the question correctly , just started learning django.
Briefly, I have 3 tables(models):
1. a "company group"
table 2. a "company" table that is linked via a foreign key to the "company group" table. That is, a group of companies includes some companies.
3. table "orders". I haven't finished it yet, so far 3 fields. "street", "company groups" and "companies".
So far, I fill in the data only in the admin panel.
The essence of the question: can I select a specific company when entering, and the group of companies in which the company is located will automatically substitute itself or give me the choice in the singular of the desired "group of companies". And also, I would like to do the opposite, first select a group of companies, and then I would have a choice of a list of those companies that are included in this group of companies?

604232cff12cd151606617.png

604232da98dd0200662059.png

604232e9aef62177944862.png

604232f44dc39580537369.png

My models:

from django.db import models


class group_of_companies(models.Model):
    name_group_companies = models.CharField(max_length=50, verbose_name='Название группы компаний')

    def __str__(self):
        return self.name_group_companies

    class Meta:
        verbose_name = 'Группы компаний'  # единственное число
        verbose_name_plural = 'Группы компаний'  # множественное число
        ordering = ['-name_group_companies']


class companies(models.Model):
    company_name = models.CharField(max_length=50, verbose_name='Название компании')
    group = models.ForeignKey(group_of_companies, verbose_name='Название компании', on_delete=models.PROTECT)

    def __str__(self):
        return self.company_name

    class Meta:
        verbose_name = 'Компания'  # единственное число
        verbose_name_plural = 'Компании'  # множественное число


class orders(models.Model):
    street = models.CharField(max_length=50, verbose_name='Улица')
    order_group = models.ForeignKey(group_of_companies, verbose_name='Группа компаний',
                                    on_delete=models.PROTECT)
    order_company = models.ForeignKey(companies, verbose_name='Компания', on_delete=models.PROTECT)

    def __str__(self):
        return self.street

    class Meta:
        verbose_name = 'Заказ'  # единственное число
        verbose_name_plural = 'Заказы'  # множественное число


That is, on the 4th screen there should be a choice only from companies number 1 and 2, since they are included in the first group of companies.
I would be grateful for any help!
Thank you for your attention.

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