user14174047
user14174047

Reputation:

Two Django Projects with One Database

I built a dashboard with Django that all my customers use. I want to build an internal dashboard for myself so that I can monitor their activity. It should connect to the same database as the dashboard. I have a a question about how to do this:

Do I just add the same database in settings.py and re-define the models in the internal dashboard?

Thanks! If there's a better way to do this, let me know!

Upvotes: 2

Views: 2617

Answers (2)

Dipak Savaliya
Dipak Savaliya

Reputation: 1

#simply run this code in terminal and write app name in which you want to access database tables

python manage.py inspectdb > your_app_name/models.py

Upvotes: 0

alamshafi2263
alamshafi2263

Reputation: 659

Multiple Django projects can share single database. There is no harm in that. Only thing to keep in mind that, only one of the projects should run migrations for the sake of simplicity.

You can generate the models.py file for your internal application by running the following command from your original application

python manage.py inspectdb > models.py

This allows you to generate a Django models file from an existing database. By default inspectdb generates models with Model.Meta.managed = False i.e. this particular application where generated model is going to be used, is not responsible for creation, deletion or update of the database table. In case of generated models you might need to configure the relationships since, inspectdb generates all relationships as ForeignKey and generates through tables to maintain relationships. You might also need to provide related_names based on your requirements.

In summary, you can use the same databse(s) across multiple django projects. More in the docs

Upvotes: 5

Related Questions