Opened 10 years ago

Closed 10 years ago

#23496 closed Uncategorized (fixed)

Model not detected as installed if custom model path is used

Reported by: yscumc Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a model at this python path: my_path.my_app.models.common.User

In my_path/my_app/models/__init__.py, there's an import line like this:

from .common import User

In settings.py, I have

INSTALLED_APPS = {
    # ...
    'my_path.my_app',
    # ...
}
# ...
AUTH_USER_MODEL = 'my_app.User'

This seems to work for almost all cases and the model is accessible as my_path.my_app.models.User

However, South breaks on creating the initial migration with the following message:

CommandError: One or more models did not validate:
auth.user: Model has been swapped out for 'my_app.User' which has not been installed or is abstract.

Upon further debugging and loading up the shell, I realized that Django reports the model as not installed:

>>> from my_path.my_app.models import User
>>> User._meta.installed
False

In django/db/models/options.py line 80, I see this line:

self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS

And testing this in the shell:

>>> User.__module__
'my_path.my_app.models.common'
>>> import re
>>> re.sub('\.models$', '', 'my_path.my_app.models.common')
'my_path.my_app.models.common'
>>>

The following regex seems to work in my case, but a more robust method of detection should probably be used in case the actual model exists in a totally different python path.

>>> re.sub('\.models($|\..*)', '', 'my_path.my_app.models.common')
'my_path.my_app'

(Currently using 1.6.7, but I noticed this problem back in Django 1.5 as well.)

Change History (2)

comment:1 by Tim Graham, 10 years ago

Can you test on Django 1.7? Application loading has been refactored and the re.sub logic no longer exists. We won't be fixing the issue on 1.6 as that branch is only receiving security fixes, so if there is no issue on 1.7, we can close this ticket. Thanks.

comment:2 by Baptiste Mispelon, 10 years ago

Resolution: fixed
Status: newclosed

I can reproduce the described issue and confirm that it's indeed fixed in 1.7.

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