0

I want to build a Django application in which each "set of users" can see different data, as they were using different DBs. Is there any best practice to achieve this?

E.g. user A, after logging, sees and manages his own data, with no possibility to see other's data.

I could use Django multi-db feature to define several dbs and create an automatic router to point to the correct db according to the user of current request (keeping auth_user on a common db).

As last chance, I can make a different application folder for each group (the only difference being the database name), but I prefer a smarter solution.

Don
  • 16,928
  • 12
  • 63
  • 101
  • What's the question? What sort of data? Are you using django.auth to manage users? Have you got user profiles? Are you using django.auth groups? This is far too vague a question to be answerable. – Timmy O'Mahony Apr 18 '12 at 11:39

1 Answers1

1

You could consider reusing one of the many per-object permission apps for django.

Another possibility is to make different settings files. For example, settings_groupA.py:

from settings import *
DATABASES = ...

And then you could use management commands, like syncdb, or runserver, etc, etc ... with the --settings option:

--settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.

Examples:

./manage.py syncdb --settings=settings_groupA
./manage.py runserver --settings=settings_groupA

The advantage of using one database per set of users is that you can keep your code easier and let them have native support for django.contrib.admin with no hack. But then if you're going to have per-object permissions within a group anyway then you might as well go straight for the first solution.

jpic
  • 32,891
  • 5
  • 112
  • 113
  • I'm actually setting my mind on using multiple databases via an Automatic Database Router which switches db according to the user. Do you think it is a good idea? (I'm inserting this in the question) – Don Apr 18 '12 at 12:10
  • 1
    Maybe if you query the users database you could switch the connection cursor for the request to another database. But that's going to require a lot more care than just using one django instance per database so I would definitively think twice before heading towards this direction. – jpic Apr 18 '12 at 12:24