Django - model in multiple apps (multiple tables)

Viewed 147

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.

  • Why does it not happen?
  • What would I have to do to achieve this?

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

1 Answers

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): ...

Related