db.models.base.ModelBase.__new__ fails to deduce app_label with model packages
|Reported by:||Owned by:||nobody|
|Severity:||major||Keywords:||egg, distutils, pkg_resources|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
In the latest magic-removal branch, if models are in a separate directory (package) instead of just a module they will have one more component in their path.
db.models.base.ModelBase.__new__ tries to deduce the app_label from the model's module, but fails in this case. It will call
register_module() with the parent directory as its app_label, but in this case the parent directory is the model package itself, resulting in it registering the module wrongly. The code in question is:
if getattr(new_class._meta, 'app_label', None) is None: # Figure out the app_label by looking one level up. # For 'django.contrib.sites.models', this would be 'sites'. new_class._meta.app_label = model_module.__name__.split('.')[-2]
For example, a model in
myproj/apps/myapp/models/mymodel.py will register itself as belonging to
myproj.apps.myapp.models with app_label models.