#1437 closed defect (invalid)
[patch] Allow models to live outside models.py
Reported by: | Luke Plant | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Metasystem | Version: | magic-removal |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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:
some_random_file.py
:
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'.
Attachments (1)
Change History (6)
by , 19 years ago
Attachment: | enable_models_outside_models_py.diff added |
---|
comment:1 by , 19 years ago
(you probably mean myproj.myapp.models
and myproj/myapp/models
above, not myapp/myproj...)
comment:2 by , 19 years ago
Actually, I've changed my mind about this patch. For one thing, I have just discovered doing 'foo??
' in ipython to get the complete source of 'foo
', which is amazingly cool, and this patch breaks it. Bad Patch!
The other is this: the patch is just a way to get round the test on INSTALLED_APPS that was recently added. The question is - why was this test added? Is it the right test? The test is in essence saying: all models *have* to live in models.py, (and that models.py has to be in INSTALLED_APPS). The problem is I don't want that behaviour -- I want to be able to put the models in different files, and I think Django needs this flexibility.
comment:3 by , 19 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:4 by , 19 years ago
To split models to multiple files, you can do the following (works at least with revision 2819):
apps/polls/models/__init__.py
:
from poll import Poll from choice import Choice
apps/polls/models/poll.py
:
from django.db import models class Poll(models.Model): question = models.CharField(maxlength=200) pub_date = models.DateTimeField('date published') class Meta: app_label = 'polls'
apps/polls/models/choice.py
:
from django.db import models from poll import Poll lass Choice(models.Model): poll = models.ForeignKey(Poll) choice = models.CharField(maxlength=200) votes = models.IntegerField() class Meta: app_label = 'polls'
Patch to implement this ticket