Opened 8 years ago
Closed 8 years ago
#27265 closed Bug (invalid)
Using @admin.register causes failure when AdminModel constructor is overriden
Reported by: | Diego Andrés Sanabria Martín | Owned by: | nobody |
---|---|---|---|
Component: | contrib.admin | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
When trying to override the ModelAdmin constructor using the decorator @admin.register I get an exception.
This is an example for a new project called "what" with an simple app "why":
Commands to create the files (I used django 1.8.4):
django-admin stratproject what
cd what
python manage.py startapp why
what/settings.py
... INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'why', ) ...
why/models.py
from django.db import models # Create your models here. class WhyMe(models.Model): name = models.CharField(max_length=255)
why/admin.py
from django.contrib import admin from why.models import WhyMe @admin.register(WhyMe) class WhyMeAdmin(admin.ModelAdmin): def __init__(self, *args, **kwargs): super(WhyMeAdmin, self).__init__(*args, **kwargs) print("Whaaaaatttt???")
If I run the command 'runserver' I've got:
nhandled exception in thread started by <function wrapper at 0x10ae3b758> Traceback (most recent call last): File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run autoreload.raise_last_exception() File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception six.reraise(*_exception) File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper fn(*args, **kwargs) File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/__init__.py", line 27, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate app_config.ready() File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/contrib/admin/apps.py", line 23, in ready self.module.autodiscover() File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover autodiscover_modules('admin', register_to=site) File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules import_module('%s.%s' % (app_config.name, module_to_search)) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/Users/diegueus9/.virtualenvs/django1.8/what/why/admin.py", line 6, in <module> class WhyMeAdmin(admin.ModelAdmin): File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/contrib/admin/decorators.py", line 28, in _model_admin_wrapper admin_site.register(models, admin_class=admin_class) File "/Users/diegueus9/.virtualenvs/django1.8/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 108, in register admin_obj = admin_class(model, self) File "/Users/diegueus9/.virtualenvs/django1.8/what/why/admin.py", line 8, in __init__ super(WhyMeAdmin, self).__init__(*args, **kwargs) NameError: global name 'WhyMeAdmin' is not defined
However, if I use the normal registration 'admin.site.register(WhyMe, WhyMeAdmin)' everything works fine.
This also happens with latest 1.8.x, 1.9.x and 1.10.x.
Also I noticed that the print statement is executed twice and I'm not sure why and if that is another bug.
Change History (2)
comment:1 by , 8 years ago
Description: | modified (diff) |
---|---|
Version: | 1.10 → master |
comment:2 by , 8 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
It's a documented limitation: #24323.