40👍
I’d do the following:
myproject/
...
app1/
views.py
__init__.py
models.py
submodels/
__init__.py
model1.py
model2.py
app2/
views.py
__init__.py
models.py
submodels/
__init__.py
model3.py
model4.py
Then
#myproject/app1/models.py:
from submodels/model1.py import *
from submodels/model2.py import *
#myproject/app2/models.py:
from submodels/model3.py import *
from submodels/model4.py import *
But, if you don’t have a good reason, put model1 and model2 directly in app1/models.py and model3 and model4 in app2/models.py
—second part—
This is app1/submodels/model1.py file:
from django.db import models
class Store(models.Model):
class Meta:
app_label = "store"
Thus correct your model3.py file:
from django.db import models
from app1.models import Store
class Product(models.Model):
store = models.ForeignKey(Store)
class Meta:
app_label = "product"
Edited, in case this comes up again for someone:
Check out django-schedule for an example of a project that does just this.
https://github.com/thauber/django-schedule/tree/master/schedule/models
https://github.com/thauber/django-schedule/
194👍
For anyone on Django 1.9, it is now supported by the framework without defining the class meta data.
https://docs.djangoproject.com/en/1.9/topics/db/models/#organizing-models-in-a-package
NOTE: For Django 2, it’s still the same
The
manage.py startapp
command creates an application structure that includes a models.py file. If you have many models, organizing them in separate files may be useful.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.
So, in your case, for a structure like
app1/
views.py
__init__.py
models/
__init__.py
model1.py
model2.py
app2/
views.py
__init__.py
models/
__init__.py
model3.py
model4.py
You only need to do
#myproject/app1/models/__init__.py:
from .model1 import Model1
from .model2 import Model2
#myproject/app2/models/__init__.py:
from .model3 import Model3
from .model4 import Model4
A note against importing all the classes:
Explicitly importing each model rather than using
from .models import *
has the advantages of not cluttering the namespace, making code more readable, and keeping code analysis tools useful.
- [Django]-Django limit_choices_to for multiple fields with "or" condition
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-How do I get user IP address in Django?
11👍
I’ve actually come across a tutorial for exactly what you’re asking about, you can view it here:
https://web.archive.org/web/20190331105757/http://paltman.com/breaking-apart-models-in-django/
One key point that’s probably relevant – you may want to use the db_table field on the Meta class to point the relocated classes back at their own table.
I can confirm this approach is working in Django 1.3
- [Django]-Django ManyToMany filter()
- [Django]-Django gunicorn sock file not created by wsgi
- [Django]-Django import error – No module named core.management
9👍
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
- [Django]-How can I get all the request headers in Django?
- [Django]-Adding to the "constructor" of a django model
- [Django]-Django select_for_update cannot be used outside of a transaction
1👍
Easiest Steps :
- Create model folder in your app (Folder name should be model)
- Delete model.py file from app directory (Backup the file while you delete it)
- And after create init.py file in model folder
- And after init.py file in write simple one line
- 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’
- And after in model file define your database table same as write like in model.py file
- 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
- [Django]-Django: accessing session variables from within a template?
- [Django]-How do I reuse HTML snippets in a django view
- [Django]-Django development server reload takes too long
0👍
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
- [Django]-STATIC_ROOT vs STATIC_URL in Django
- [Django]-Django DRF with oAuth2 using DOT (django-oauth-toolkit)
- [Django]-Python + Django page redirect