[patch] Allow models to live outside models.py
|Reported by:||lukeplant||Owned by:||adrian|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
In current magic-removal, you get exceptions if you want to place a model outside of the models.py module (e.g. in a subpackage) (to be specific, the check that the model is in INSTALLED_APPS throws an exception). The attached patch fixes this by allowing 'module' to be specified as a 'Meta' option.
Previously using 'app_label' would fix it. By specifying 'module' instead, you won't need 'app_label' (in the usual case at least). Meta.module should be the fully qualified module name of the models file e.g. 'myproj.myapp.models', and you also need to import your model classes into models.py or models/__init__.py.
It looks like this:
class Foo(models.Model): # fields etc class Meta: module = 'myapp.myproj.models'
myapp/myproj/models.py or myapp/myproj/models/__init__.py:
from some_random_file import Foo
I think that's relatively clean. It only requires one line extra per model than what you would usually do i.e. the 'module' option in 'Meta'.
Change History (6)
Changed 10 years ago by lukeplant
comment:3 Changed 10 years ago by lukeplant
- Resolution set to invalid
- Status changed from new to closed