Loading docs/topics/db/models.txt +24 −0 Original line number Diff line number Diff line Loading @@ -1366,6 +1366,30 @@ different database tables). Django will raise a :exc:`~django.core.exceptions.FieldError` if you override any model field in any ancestor model. Organizing models in a package ============================== The :djadmin:`manage.py startapp <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. For example, if you had ``organic.py`` and ``synthetic.py`` in the ``models`` directory: .. snippet:: :filename: myapp/models/__init__.py from .organic import Person from .synthetic import Robot 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. .. seealso:: :doc:`The Models Reference </ref/models/index>` Loading Loading
docs/topics/db/models.txt +24 −0 Original line number Diff line number Diff line Loading @@ -1366,6 +1366,30 @@ different database tables). Django will raise a :exc:`~django.core.exceptions.FieldError` if you override any model field in any ancestor model. Organizing models in a package ============================== The :djadmin:`manage.py startapp <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. For example, if you had ``organic.py`` and ``synthetic.py`` in the ``models`` directory: .. snippet:: :filename: myapp/models/__init__.py from .organic import Person from .synthetic import Robot 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. .. seealso:: :doc:`The Models Reference </ref/models/index>` Loading