Opened 8 years ago

Closed 8 years ago

#26816 closed Bug (fixed)

BaseModelAdminChecks._check_inlines_item may raise AttributeError

Reported by: Keryn Knight Owned by: Berker Peksag
Component: Core (System checks) Version: 1.9
Severity: Normal Keywords:
Cc: django@…, berker.peksag@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

given something like:

class MyInline(object): pass

class MyModelAdmin(admin.ModelAdmin):
    inlines = [MyInline]

admin.E104 will be added to the error stack because its not a subclass (ugh) of BaseModelAdmin.

changing to:

class MyInline(BaseModelAdmin): pass

will throw:

AttributeError: type object 'MyInline'  has no attribute 'model'

because its not until ModelAdmin or InlineModelAdmin that the attribute is set (in __init__ for ModelAdmin, as a class attribute for InlineModelAdmin)

Encountered on 1.9, but looks to be the same in master.

Change History (4)

comment:1 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Keryn Knight, 8 years ago

Additional addendum:

class MyInline(BaseModelAdmin):
    model = MyModel

will also fail it seems, because BaseModelAdmin doesn't have the same __init__ argspec as InlineModelAdmin:

TypeError: __init__() takes 1 positional argument but 3 were given

I suppose the simplest fix is to check the inline is a subclass of InlineModelAdmin.

comment:3 by Berker Peksag, 8 years ago

Cc: berker.peksag@… added
Has patch: set
Owner: changed from nobody to Berker Peksag
Status: newassigned

For future reference, here is the full traceback when BaseModelAdmin is used as a base class (see comment 2 for full example):

Unhandled exception in thread started by <function wrapper at 0x2f98668>
Traceback (most recent call last):
  File "/home/berker/projects/django/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/berker/projects/django/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/home/berker/projects/django/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/berker/projects/django/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/berker/projects/django/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/berker/projects/django/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/berker/projects/django/django/contrib/admin/apps.py", line 23, in ready
    self.module.autodiscover()
  File "/home/berker/projects/django/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/berker/projects/django/django/utils/module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/berker/projects/testdjango/core/admin.py", line 38, in <module>
    site.register(SlugModel, SlugAdmin)
  File "/home/berker/projects/django/django/contrib/admin/sites.py", line 110, in register
    system_check_errors.extend(admin_obj.check())
  File "/home/berker/projects/django/django/contrib/admin/options.py", line 117, in check
    return self.checks_class().check(self, **kwargs)
  File "/home/berker/projects/django/django/contrib/admin/checks.py", line 519, in check
    errors.extend(self._check_inlines(admin_obj))
  File "/home/berker/projects/django/django/contrib/admin/checks.py", line 557, in _check_inlines
    for index, item in enumerate(obj.inlines)
  File "/home/berker/projects/django/django/contrib/admin/checks.py", line 585, in _check_inlines_item
    return inline(model, obj.admin_site).check()
TypeError: __init__() takes exactly 1 argument (3 given)

PR. test_not_model_admin and test_missing_model_field in tests/modeladmin/tests.py already tested the behaviors demonstrated by kezabelle.

comment:4 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 426bca00:

Fixed #26816 -- Corrected an admin check to require inlines to subclass InlineModelAdmin.

Note: See TracTickets for help on using tickets.
Back to Top