Best way to develop models for a specific organization in Django?

It is a matter of designing a database model. Say I'm developing an application like Slack. Slack has several organizations, and within each Organization there are objects to which only this Organization should be accessible (for example, its chat entries, files, etc.). What is the best way to configure these objects for organization in Django?

A simple solution is to attach a ForeignKey to each of these objects. For instance:

class Organization(models.Model): # ... class File(models.Model): organization = models.ForeignKey( 'Organization', on_delete=models.CASCADE, ) # ... class ChatThread(models.Model): organization = models.ForeignKey( 'Organization', on_delete=models.CASCADE, ) # ... 

But if we do this, we need to put an index on the organization , and since there are many such objects for the organization, this seems a little wasteful.

Is there a cleaner way to develop it?

+5
source share
4 answers

I think your method is about as good as it should be. Regarding indexing an organization column, you can use db_index=False to disable index creation.

If you want to abstract the organization field and have some methods for all objects of the organization, you can use the abstract model as follows:

 class Organization(models.Model): # ... class OrganizationModel(models.Model): organization = models.ForeignKey( 'Organization', on_delete=models.CASCADE, db_index=False, ) class Meta: abstract = True class File(OrganizationModel): # ... class ChatThread(OrganizationModel): # ... 
+5
source

Your method is clean, if each Organisation can use the same File , then you should use models.ManytoManyField , but I doubt that Slack works like that.

In addition, using Slack, you can access files from anywhere, but every file you use may not always be published for each stream only.

The structure of the model you propose seems to be the best for what you are trying to achieve, if you have people in each organization using your application, then you may need to develop a new model for each person.

This is what I need:

 class Organization(models.Model): # ... #each person is part of an organization class Person(models.Model): organization = models.ForeignKey('Organization', on_delete=models.CASCADE) # ... #each file is part of an organization class File(models.Model): organization = models.ForeignKey('Organization', on_delete=models.CASCADE) # ... #each thread is part an Organization #each thread can have many users, each user can join many thread. #each thread can have many files, each file can be shared across one or many thread class ChatThread(models.Model): organization = models.ForeignKey('Organization', on_delete=models.CASCADE) people = models.ManyToManyField(Person, ...) files = models.ManyToManyField(File, ...) # ... 
+2
source

For me, the true question is:

Was there always an organization in your file models? Same question for ChatThread models?

If not, you should not use ForeignKey, but ManyToManyField. And then you avoid model dependencies.

Otherwise, you are set up correctly.

0
source

I'm not sure about this, but if you are developing an application like Slack, I think you need to develop it using Tenants, for example: customer.domain.com, it’s better to organize the information.

In your base organization, you can do this, as in this example:

 # The Organization Model class Organization(models.Model): # ... #each person is part of an organization class Person(models.Model): organization = models.ForeignKey('Organization', on_delete=models.CASCADE) # ... #each file is part of an organization class File(models.Model): organization = models.ForeignKey('Organization', on_delete=models.CASCADE) # ... #each thread is part an Organization #each thread can have many users, each user can join many thread. #each thread can have many files, each file can be shared across one or many thread class ChatThread(models.Model): organization = models.ForeignKey('Organization', on_delete=models.CASCADE) people = models.ManyToManyField(Person, ...) files = models.ManyToManyField(File, ...) # ... 
0
source

Source: https://habr.com/ru/post/1275937/


All Articles