lundi 25 octobre 2021

Django Model Design Difference: link multiple simple models

I am designing Django models and would like some feedback on differences between different design schemas. Also, currently I am working only with Django Admin so my confusion might be coming from there.

Let's suppose I have model Industry, which has multiple Locations.

Banking -> US
Banking -> UK
Banking -> Japan
...

Also, I have another model where industry and location is selected by user (on the website). So, if Banking is selected I want to show only locations that are available for banking. Here's the basic design I have already implemented (for demo purpose):

class Industry(models.Model):
    """ Model representing industries. """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID for this industry and location')
    name = models.CharField(blank=False,null=True,max_length=100,verbose_name="Industry")
    location = models.CharField(blank=False,null=True,max_length=100,verbose_name="Location")

    def __str__(self):
        field_values = get_val(self) #this functions works
        return ' '.join(field_values)

    class Meta:
        db_table = 'Industry'


class Select(models.Model):
    """ Model Select """
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, help_text='Unique ID Select')
    industry = models.ForeignKey('Industry',on_delete=models.SET_NULL,related_name='+',blank=False,null=True,max_length=100,verbose_name="Industry")
    location = models.CharField(blank=False,null=True,max_length=100,verbose_name="Location")

    def __str__(self):
        field_values = get_val(self)
        return ' '.join(field_values)

    class Meta:
        db_table = 'Select'

As a superadmin, let's suppose in Industry model I create Banking->USA, Banking->UK and in Select model I want to choose Banking, and for the location I want to select from available locations for Banking.

How should I change each of those models and achieve what I am trying to do?




Aucun commentaire:

Enregistrer un commentaire