#25127: document how to define models in multiple modules
--------------------------------------+------------------------------------
Reporter: keturn | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by aaugustin):
Indeed, Django 1.7's app-loading process is good at figuring out which app
a given model belongs to, as long as you organize your code in a sensible
way. To be safe, you should ensure that importing `path.to.app.models` has
the side-effect of importing all models belonging to that app.
Not doing so will generally work in practice, but in theory it could lead
to inconsistencies in the app-cache registry. For instance, you may be
unable to query relations involving these models until they get imported,
perhaps by a later HTTP request. This could manifest as random errors in
production for a short time after restarting the server. You don't want to
face such issues.
Technically, you don't need to import the model classes themselves into
`path.to.app.models`. You just need to import the modules that defines
them so that the model classes get created in the memory of your Python
process, at which point they register themselves with the app registry.
However, it's a good idea to ensure that `from path.to.app.models import
WhateverModel` works for any model, because that's one of the most common
import patterns in Django projects. For this reason, I think we should
encourage always importing all models into the `models` package when
they're defined in submodules.
--
Ticket URL: <https://code.djangoproject.com/ticket/25127#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/064.ca4d0a573e54c49f8823587083aa2bcb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.