Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#1437 closed defect (invalid)

[patch] Allow models to live outside

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


In current magic-removal, you get exceptions if you want to place a model outside of the 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 or models/

It looks like this:

class Foo(models.Model):
    # fields etc
    class Meta:
        module = 'myapp.myproj.models'

myapp/myproj/ or myapp/myproj/models/

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)

enable_models_outside_models_py.diff (1.8 KB) - added by Luke Plant 13 years ago.
Patch to implement this ticket

Download all attachments as: .zip

Change History (6)

Changed 13 years ago by Luke Plant

Patch to implement this ticket

comment:1 Changed 13 years ago by Antti Kaihola

(you probably mean myproj.myapp.models and myproj/myapp/models above, not myapp/myproj...)

comment:2 Changed 13 years ago by Luke Plant

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, (and that 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 Changed 13 years ago by Luke Plant

Resolution: invalid
Status: newclosed

I've closed this in favour of reverting the change that made it necessary [2406] - see #1179 for details.

comment:4 Changed 12 years ago by Antti Kaihola

To split models to multiple files, you can do the following (works at least with revision 2819):


from poll import Poll
from choice import Choice


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'


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'

comment:5 Changed 12 years ago by (none)

milestone: Version 1.0

Milestone Version 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top