Tobias Gårdner
Tobias Gårdner

Reputation: 163

Django with multiple Apps and reusing the same database tables

I am quite new to Django. Right now I am working on a prototype with ~30 database tables. I have understood that with Django you preferably want to have quite small Apps, with limited number of Models.

I want to use the same Models (i.e. database tables) in several different Apps. What is the best practice to achieve this? I am using mysql.

Upvotes: 4

Views: 3842

Answers (1)

Erdem
Erdem

Reputation: 62

might be quite late for your question. You may use same model by different app just by refering to it. First your apps should be under the same project. Choose one app as 'core' and define your models under it,(I have read that models are better to be at app level) Then whenever you would like to use a model from that app, import that app's model at the beginning of the code. Let say we have two apps: app_1 and app_2 and a model defined under app_1/models.py: model_1

In app_2/views.py , import model_1

from app_1.models import model_1

Upvotes: 5

Related Questions