J
J
Jekson2020-02-27 12:27:44
Django
Jekson, 2020-02-27 12:27:44

Selecting relationships between tables in a database?

Prompt on correctness of a choice of architecture of a DB under the task. The meaning is the following

There is a Vendors model - in fact it is a commercial organization, there can be many of them

class Vendors(models.Model):
    name...
    country ....


There is a global Modules model - it describes 10 variants of existing modules. It is possible to make a module inactive globally, then there is no access to it.

class Modules(models.Model):
    MODULES_NAME = (
                    ("Sourcing", "Sourcing"), ("SA", "SA"),
                    ("SXM", "SXM"), ("CLM", "CLM"),
                    .....
                    )
    module_name = models.CharField(max_length=50, choices=MODULES_NAME)
    active = models.BooleanField(default=True)


The VendorModules table that links Vendor and Modules with the ability to disable a specific module or multiple modules relative to a vendor. Ie disable the module is not at the global level.

class VendorModule(models.Model):
    vendor = models.OneToOneField('Vendors', on_delete=models.CASCADE, primary_key=True)
    active = models.BooleanField(default=True)


Model Element - contains information related to a specific module of a specific vendor.

class Element(models.Model):
    info1...
    info2.....
    ....


As a result, a new vendor is added to the system, associates itself with some modules -> introduces information on these modules -> can disable association with some modules. How to make connections between models?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2020-02-27
@Lepilov

I propose the following scheme:

class Vendor(models.Model):
    ...

class Module(models.Model):
    MODULES_NAME = (
        ("Sourcing", "Sourcing"), ("SA", "SA"),
        ("SXM", "SXM"), ("CLM", "CLM"),
        ...
    )
    name = models.CharField(max_length=50, choices=MODULES_NAME)
    is_active = models.BooleanField(default=True)


class VendorModule(models.Model):
    vendor = models.ForeignKey(Vendor, on_delete=models.CASCADE)
    module = models.ForeignKey(Module, on_delete=models.CASCADE)
    is_active = models.BooleanField(default=True)
    info1 = models.TextField(max_length=500)
    info2 = models.TextField(max_length=500)

Communication of modules with vendors is carried out through VendorModule- in fact, this is a many-to-many connection with the ability to save additional information. The purpose of the model Elementis not clear, so additional information is removed in VendorModule. But if you think that this is not enough, then you can do this:
class Element(models.Model):
    vendormodule = models.ForeignKey(VendorModule, on_delete=models.CASCADE)
    info1 = models.TextField(max_length=500)
    info2 = models.TextField(max_length=500)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question