ProfHase85
ProfHase85

Reputation: 12173

Django - model in multiple apps (multiple tables)

I am trying to import a model from one app into another creating a new table etc.

# app1/models.py

class MyModel(models.Model):
   myfield = models.CharField(...)
   ...

# app2/models/__init__.py

from app1.models import MyModel as MyShadowModel

What I would expect: python manage.py makemigrations would see changes in app2 and create the according tables.

Background

A Django project shall process Data and make changes visible. It consists of two apps:

app1 contains already processed data app2 processes changes and shall show what data in app1 would look like.

The idea: app2 processes the data into a "shadow" model. Therefore i would like to use the exact same model as app1 in my models in app2

Upvotes: 1

Views: 476

Answers (1)

lucutzu33
lucutzu33

Reputation: 3700

Importing the model doesn't suffice. Imagine you only have to do some logic with MyModel in app2's models.py. It doesn't make sense to create another table.

You have to declare another model that inherits from the parent model in order to replicate it.

from app1.models import MyModel

class MyShadowModel(MyModel): ...

Upvotes: 3

Related Questions