J
J
Jekson2020-05-13 17:30:51
Django
Jekson, 2020-05-13 17:30:51

How to create an additional table for links between models?

There are two models Module (module) and ParentCategory (categories). they are not related to each other in any way.

class Modules(models.Model):
    .....

class ParentCategory(models.Model):
    ....


I want to make a connection between them according to the following principle. One module contains several categories. I described this structure with a dictionary where the key of the dictionary is the name of the module and the value is a list of categories belonging to this module.
pc_to_modules_assign = {
                            "Strategic Sourcing": ['COMMON S2P', 'COMMON SOURCING - SXM', 'SERVICES', 'SOURCING'],
                            "Supplier Management": ['COMMON S2P', 'COMMON SOURCING - SXM', 'SERVICES', 'SXM'],
                            "Spend Analytics": ['COMMON S2P', 'SERVICES', 'Spend Analytics'],
                            "Contract Management": ['COMMON S2P', 'SERVICES', 'CLM'],
                            "e-Procurement": ['COMMON S2P', 'SERVICES', 'eProcurement'],
                            "Invoice-to-Pay": ['COMMON S2P', 'SERVICES', 'I2P'],
                            "AP Automation": ['COMMON S2P', 'SERVICES', 'I2P', 'AP'],
                            "Strategic Procurement Technologies": ['COMMON S2P', 'COMMON SOURCING - SXM', 'SERVICES',
                                                                   'SOURCING', 'SXM', 'Spend Analytics', 'CLM'],
                            "Procure-to-Pay": ['COMMON S2P', 'SERVICES', 'eProcurement', 'I2P'],
                            "Source-to-Pay": ['COMMON S2P', 'COMMON SOURCING - SXM', 'SERVICES',
                                              'SOURCING', 'SXM', 'Spend Analytics', 'CLM', 'eProcurement', 'I2P']
                             }


There is one main category in the list of categories - primary. Prompt what connections should be used in a similar model or what kind of fields are needed. Something like

class PCtoModules(models.Model):
m = .....Modules
pc = ....ParentCategory
primary = models.BooleanField(default=False)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bubaley, 2020-05-13
@Lepilov

To solve this problem, you need to use ManyToMany relation.
Add a line to the Modules class

parent_categories= models.ManyToManyField('ParentCategory', related_name='modules', through='ModuleParentCategory')

And adds a throught table
class ModuleParentCategory(models.Model):
    module = models.ForeignKey(Module, on_delete=models.CASCADE)
    parent_category = models.ForeignKey(ParentCategory, on_delete=models.CASCADE)
    primary = models.BooleanField(default=False)

To fill in or edit this relationship, you can read the ManyToMany with throught documentation
https://django.fun/docs/django/ru/3.0/topics/db/models/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question