T
T
Thrashches2020-04-05 22:51:02
Django
Thrashches, 2020-04-05 22:51:02

Django. What is the best way to organize connections?

I am designing a database of equipment. There was a question of the correct organization of communications between models.
At the moment the code is as follows:

class Person(models.Model):
    fullname = models.CharField(max_length=30)
   #shop_accounts = models.ManyToManyField('ShopAccount', blank=True)
   #stuff = models.ManyToManyField('Staff', blank=True)  
    def __str__(self):
        return str(self.fullname)


class Shop(models.Model):
    name = models.CharField(max_length=30)
    url = models.CharField(max_length=100)
#accounts?
#persons?

def __str__(self):
        return str(self.name)


class ShopAccount(models.Model):
    login = models.CharField(max_length=30)
    password = models.CharField(max_length=100)
    person = models.ForeignKey(Person, on_delete=models.CASCADE)#on_delete=models.SET(Person)
    shop = models.ForeignKey(Shop, on_delete=models.CASCADE)
    
    def __str__(self):
        return str(self.login)


class Stuff(models.Model):
    name = models.CharField(max_length=30)
    person = models.ForeignKey(Person, on_delete=models.CASCADE)#on_delete=models.SET(Person)
    shop = models.ForeignKey(Shop, on_delete=models.CASCADE)
    shop_account = models.ForeignKey(ShopAccount, on_delete=models.CASCADE)
    comment = models.CharField(max_length=140, blank=True)
    #here will be additional attributes

    def __str__(self):
        return str(self.name)

I would like that when filling out the Stuff table, when choosing a store (Shop), I would see only those persons (Person) who bought in it, and the corresponding account would be rigidly linked to the store and person. I think that it is more correct to solve this at the level of models, and not in the frontend. How would it be better to organize the relations between these models in this case?

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