Split models.py into several files

Viewed 69528

I'm trying to split the models.py of my app into several files:

My first guess was do this:

myproject/
    settings.py
    manage.py
    urls.py
    __init__.py
    app1/
        views.py
        __init__.py
        models/
            __init__.py
            model1.py
            model2.py
    app2/
        views.py
        __init__.py
        models/
            __init__.py
            model3.py
            model4.py

This doesn't work, then i found this, but in this solution i still have a problem, when i run python manage.py sqlall app1 I got something like:

BEGIN;
CREATE TABLE "product_product" (
    "id" serial NOT NULL PRIMARY KEY,
    "store_id" integer NOT NULL
)
;
-- The following references should be added but depend on non-existent tables:
-- ALTER TABLE "product_product" ADD CONSTRAINT "store_id_refs_id_3e117eef" FOREIGN KEY     ("store_id") REFERENCES "store_store" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "product_product_store_id" ON "product_product" ("store_id");
COMMIT;

I'm not pretty sure about this, but i'm worried aboout the part The following references should be added but depend on non-existent tables:

This is my model1.py file:

from django.db import models

class Store(models.Model):
    class Meta:
        app_label = "store"

This is my model3.py file:

from django.db import models

from store.models import Store

class Product(models.Model):
    store = models.ForeignKey(Store)
    class Meta:
        app_label = "product"

And apparently works but i got the comment in alter table and if I try this, same thing happens:

class Product(models.Model):
    store = models.ForeignKey('store.Store')
    class Meta:
        app_label = "product"

So, should I run the alter for references manually? this may bring me problems with south?

6 Answers

The relevant link for Django 3 is:

https://docs.djangoproject.com/en/3.2/topics/db/models/#organizing-models-in-a-package

Links to previous versions of documentation are broken. The example there is very succinct:

To do so, create a models package. Remove models.py and create a myapp/models/ directory with an init.py file and the files to store your models. You must import the models in the init.py file.

For example, if you had organic.py and synthetic.py in the models directory:

from .organic import Person
from .synthetic import Robot

Easiest Steps :

  1. Create model folder in your app (Folder name should be model)
  2. Delete model.py file from app directory (Backup the file while you delete it)
  3. And after create init.py file in model folder
  4. And after init.py file in write simple one line
  5. And after create model file in your model folder and model file name should be same like as class name,if class name is 'Employee' than model file name should be like 'employee.py'
  6. And after in model file define your database table same as write like in model.py file
  7. Save it

My Code : from django_adminlte.models.employee import Employee

For your : from app_name.models.model_file_name_only import Class_Name_which_define_in_model_file


__init__.py

from django_adminlte.models.employee import Employee

model/employee.py (employee is separate model file)

from django.db import models

class Employee(models.Model):
eid = models.CharField(max_length=20)
ename = models.CharField(max_length=20)
eemail = models.EmailField()
econtact = models.CharField(max_length=15)

class Meta:
    db_table = "employee"
    # app_label = 'django_adminlte'
    
def __str__(self):
    return self.ename

I wrote a script that might be useful.

github.com/victorqribeiro/splitDjangoModels

it split the models in individual files with proper naming and importing; it also create an init file so you can import all your models at once.

let me know if this helps

Related