1

I Would like to implement a web page in Django which allow a user to dynamically create a Data Base.

The Django model stored will always be the same. (a generic user)

Scenario :

  1. The user fills up a form with fields like user.firstname, user.lastname ..
  2. Then after the submit, django creates a brand new DB on the DBMS.
  3. The model is then stored in this new DB.
  4. Django save/store this DB setting for further use.

I find out Here that it's possible to set up multiple DB's in Django but that implies that they already have been created (Which is obviously not my case).

To create my DB I can think of executing an custom SQL query directly on the DBMS like that

from django.db import connection

def createNewDB(self,id):
    with connection.cursort() as cursor:
        queryStr = "\"CREATE DATABASE " + id + "\"" 
        cursor.execute(queryStr)

But then I have no clues about :

  • How to save the new DB settings in Django DB (see below).

  • How to migrate and save the model user in the DB

    • Migrate : from django.core.management import call_command call_command("migrate", interactive=False)
    • Save model : user.save(using='db_id')

I find this post talking about how to add a connection dynamically with :

from django.db import connections
connections.databases['new-alias'] = { ... }
conn = connections['new-alias']

But again in, this case the sqlite DB exist before, so I'm not sure if it is what I need.

So is it possible to create a DB link to a model and then add it to django's database setting ?

Or should I review all my data structure ? (like one DB but multiple user model table)

I am using django 2.0.5 and postgreSQL 9.6 but I can change if the solution is not compatible (sqlite for example).

UserAt
  • 107
  • 1
  • 8
  • Have a look at https://github.com/OmniDB/OmniDB - could be the thing for you. – wiesion May 18 '18 at 14:31
  • @wiesion Thanks,so you mean that I should take a look to the source code of OmniDB ? – UserAt May 22 '18 at 08:10
  • I think you could clone the repo and set up the required environment. Based on that, continue to create your own apps in the project. – wiesion May 22 '18 at 08:13

0 Answers0