Hi. I have this concern. As suggested before filing an issue, I want to get 
some community response.

When all app models are stored within models module (models.py), the 
app_label is properly assigned the actual app name. This however does not 
work as expected when models are moved to their own package:

app/
  models/
    __init__.py
    foomodels.py
    barmodels.py

This can be rather easily fixed by explicitly supplying the all_label 
within the Meta:

class FooModel(models.Model):
    class Meta(object):
        app_lable = 'my_app_label'

It works however I feel it does not really follow the DRY principle for 
which I love Django (among many other awesome things).

I looked at Django source code and the issue is 
here<https://github.com/django/django/blob/master/django/db/models/base.py#L92>
:

if getattr(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'.
    model_module = sys.modules[new_class.__module__]
    kwargs = {"app_label": model_module.__name__.split('.')[-2]}

The app label is determined by taking the [-2] index, which is no longer 
correct when models are in a package.

The solution is to find the correct index location:

if getattr(meta, 'app_label', None) is None:
    # Figure out the app_label by looking for models within module hierarchy
    # For 'django.contrib.sites.models', this would be 'sites'.
    model_module = sys.modules[new_class.__module__]
    models_hierarchy = model_module.__name__.split('.')
    models_position = models_hierarchy.index('models')
    kwargs = {"app_label": models_hierarchy[models_position - 1]}

I am wondering if there is some religious reason not to do that or this 
might cause some bugs in other regions of Django?

Thank you

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to