Code

Ticket #1821: app_label.diff

File app_label.diff, 1.2 KB (added by mir@…, 8 years ago)

patch

Line 
1--- a/django/db/models/base.py
2+++ b/django/db/models/base.py
3@@ -40,9 +40,19 @@ class ModelBase(type):
4         model_module = sys.modules[new_class.__module__]
5 
6         if getattr(new_class._meta, 'app_label', None) is None:
7-            # Figure out the app_label by looking one level up.
8-            # For 'django.contrib.sites.models', this would be 'sites'.
9-            new_class._meta.app_label = model_module.__name__.split('.')[-2]
10+            # Perhaps there's a module attribute?
11+            try:
12+                new_class._meta.app_label = model_module.app_label
13+            except AttributeError:
14+                # Figure out the app_label by looking one level up.
15+                # For 'django.contrib.sites.models', this would be 'sites'.
16+                try:
17+                    new_class._meta.app_label = model_module.__name__.split('.')[-2]
18+                except IndexError:
19+                    raise IndexError, _(
20+"""Interactive model use not directly supported.
21+To circumvent this restriction, add an app_label attribute to each model class or to
22+the model module.""")
23 
24         # Add all attributes to the class.
25         for obj_name, obj in attrs.items():