#27265: Using @admin.register causes failure when AdminModel constructor is
overriden
-------------------------------------+-------------------------------------
Reporter: Diego Andrés | Owner: nobody
Sanabria Martín |
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Diego Andrés Sanabria Martín):
* needs_docs: => 0
* version: 1.10 => master
* needs_tests: => 0
* needs_better_patch: => 0
Old description:
> 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
New description:
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.
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27265#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.74036e25bd3328e8bfbf6c1f9598ecfc%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.