From ae4f89436f140361c9f6c5ef1cfa7875df8991f5 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Mon, 16 Jan 2006 23:44:06 +0000 Subject: [PATCH] magic-removal: Added helpful error message if a model contains a field named 'Admin' that isn't admin settings git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2020 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index d8b94f07d3..8116284192 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -32,11 +32,11 @@ class ModelBase(type): new_class.add_to_class('_meta', Options(attrs.pop('Meta', None))) new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {})) - #Figure out the app_label by looking one level up. - #FIXME: wrong for nested model modules app_package = sys.modules.get(new_class.__module__) - app_label = app_package.__name__.replace('.models', '') - app_label = app_label[app_label.rfind('.')+1:] + + # Figure out the app_label by looking one level up. + # For 'django.contrib.sites.models', this would be 'sites'. + app_label = app_package.__name__.split('.')[-2] # Cache the app label. new_class._meta.app_label = app_label @@ -108,6 +108,7 @@ class Model(object): def add_to_class(cls, name, value): if name == 'Admin': + assert type(value) == types.ClassType, "%r attribute of %s model must be a class, not a %s object" % (name, cls.__name__, type(value)) value = AdminOptions(**dict([(k, v) for k, v in value.__dict__.items() if not k.startswith('_')])) if hasattr(value, 'contribute_to_class'): value.contribute_to_class(cls, name)