﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23496	Model not detected as installed if custom model path is used	yscumc	nobody	"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.)"	Uncategorized	closed	Database layer (models, ORM)	1.6	Normal	fixed			Unreviewed	0	0	0	0	0	0
