1
myproject
 |- myapp

I have created a django project which has an app in it that does viewing , adding data to database etc. Now I want to create multiple users who can use this app with their own data. What is the best way about it.

Lets say there are 3 users user1, user2, user3. User1 works for company1 user2 works for company2 and user3 works for company3. So each user has his own data.

Currently I am planning to do this. I copy the myapp code and create 3 apps corresponding to each user. So the project directory looks something like this

myproject
  |- myapp
  |- myappuser1
  |- myappuser2
  |- myappuser3

So I now have 3 apps with similar functionality and each of the users has their own data. Now, the hurdle is how do I restrict access so that each user can see his own app. For instance, user1 should be able to only see myappuser1 contents and user2 should only see myappuser2 contents. How can I do this ? I tried creating groups in django admin but that only gives me permissions to edit the access to models under the myapp. It doesn't restrict user to see other users' pages.

Option 2: Create seperate django project for each user. This solves the user restriction but to deploy in heroku I should register each one as an app and then I have to create a unique login page through which each customer can login or else I have to give each user a different url for login.

Can you please suggest if there is a better way ?

user2390751
  • 65
  • 1
  • 12
  • If by three users, you meant three different kinds of users, then perhaps implementing role based access would help. – Anuvrat Parashar Jun 21 '20 at 17:24
  • not role based. Assume they are from different companies. – user2390751 Jun 21 '20 at 17:26
  • 2
    do you want to build a multi-tenant architecture? also if there is no overlap between the users and the apps they want to access, why not create separate projects? – Anuvrat Parashar Jun 21 '20 at 17:49
  • thanks for the word. yes multi-tenant. The users will be using almost using the same app but with different data. Option 2 is creating different projects but then I run into deployment issues for instance in heroku where I have create one heroku app for one customer – user2390751 Jun 21 '20 at 20:30
  • same app with different data : sounds like just about any other web application. Query on the basis of who is signed-in and only show them information related to them. – Anuvrat Parashar Jun 21 '20 at 22:10
  • apparently there are libraries which do this for you based on the architecture so that you dont run into issues. I am looking at those for now. – user2390751 Jun 22 '20 at 15:03

1 Answers1

0

If you are indeed looking for multi-tenant architecture, I would suggest you take a look at library django-tenant-schemas .

I believe you are using term users loosely and mean them to be users belonging to a client. So for example, you have an application that has three clients

  • A (It has users A1, A2, A3)
  • B (It has users B1, B2, B3)
  • C (It has users C1, C2, C3)

So when the user C3 logs in he/she should be able to view data only for the client C and not of client A/B.

If this truly is your requirement you can take a look at the library I have suggested before.

Rajesh Yogeshwar
  • 2,111
  • 2
  • 18
  • 37
  • Yes. Precisely. I have looked at similar libraries https://github.com/citusdata/django-multitenant for instance. There seems to be a difference in how they implement. For instance the one u shared used one database and one schema for tenant. Django-multitenant uses one single table and one schema. I am still thinking as to which architecture to use. – user2390751 Jun 22 '20 at 15:01
  • That is something for you to decide. For example, you may find a need to use celery later in project for which there are already is a library. Make your decision based on your use case and requirements. – Rajesh Yogeshwar Jun 22 '20 at 15:26