Opened 6 years ago

Closed 6 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


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 Changed 6 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 6 years ago by Keryn Knight

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 Changed 6 years ago by Berker Peksag

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/", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/berker/projects/django/django/core/management/commands/", line 113, in inner_run
  File "/home/berker/projects/django/django/utils/", line 249, in raise_last_exception
  File "/home/berker/projects/django/django/utils/", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/berker/projects/django/django/", line 27, in setup
  File "/home/berker/projects/django/django/apps/", line 115, in populate
  File "/home/berker/projects/django/django/contrib/admin/", line 23, in ready
  File "/home/berker/projects/django/django/contrib/admin/", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/berker/projects/django/django/utils/", line 50, in autodiscover_modules
    import_module('%s.%s' % (, module_to_search))
  File "/usr/lib/python2.7/importlib/", line 37, in import_module
  File "/home/berker/projects/testdjango/core/", line 38, in <module>
    site.register(SlugModel, SlugAdmin)
  File "/home/berker/projects/django/django/contrib/admin/", line 110, in register
  File "/home/berker/projects/django/django/contrib/admin/", line 117, in check
    return self.checks_class().check(self, **kwargs)
  File "/home/berker/projects/django/django/contrib/admin/", line 519, in check
  File "/home/berker/projects/django/django/contrib/admin/", line 557, in _check_inlines
    for index, item in enumerate(obj.inlines)
  File "/home/berker/projects/django/django/contrib/admin/", 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/ already tested the behaviors demonstrated by kezabelle.

comment:4 Changed 6 years ago by Tim Graham <timograham@…>

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