Opened 22 months ago

Closed 22 months ago

Last modified 22 months ago

#29723 closed Bug (fixed)

Admin crashes if InlineModelAdmin.has_add_permission() doesn't accept the obj argument

Reported by: Aymeric Augustin Owned by: Tim Graham
Component: contrib.admin Version: 2.1
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The release notes suggest that InlineModelAdmin.has_add_permission() methods that don’t accept obj as the second positional argument will be supported until Django 3.0:

Support for InlineModelAdmin.has_add_permission() methods that don’t accept obj as the second positional argument will be removed in Django 3.0.

This doesn't appear to be true in my experience.

I have this method defined on an InlineModelAdmin:

    def has_add_permission(self, request):
        return False

I'm getting this traceback with Django 2.1:

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
  607.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
  223.             return view(request, *args, **kwargs)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/contrib/admin/options.py" in add_view
  1647.         return self.changeform_view(request, None, form_url, extra_context)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/contrib/admin/options.py" in changeform_view
  1536.             return self._changeform_view(request, object_id, form_url, extra_context)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/contrib/admin/options.py" in _changeform_view
  1590.                 formsets, inline_instances = self._create_formsets(request, form.instance, change=False)

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/contrib/admin/options.py" in _create_formsets
  1945.         for FormSet, inline in self.get_formsets_with_inlines(*get_formsets_args):

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/contrib/admin/options.py" in get_formsets_with_inlines
  795.             yield inline.get_formset(request, obj), inline

File "/home/ubuntu/.pyenv/versions/3.6.2/envs/cupido/lib/python3.6/site-packages/django/contrib/admin/options.py" in get_formset
  2055.         can_add = self.has_add_permission(request, obj) if request else True

Exception Type: TypeError at /admin/cupido/notification/add/
Exception Value: has_add_permission() takes 2 positional arguments but 3 were given

Change History (7)

comment:1 Changed 22 months ago by Carlton Gibson

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Refs #27991 -- Add obj arg to InlineModelAdmin.has_add_permission()

There's a check to determine how to call has_add_permission():

            # RemovedInDjango30Warning: obj will be a required argument.
            args = get_func_args(inline.has_add_permission)
            if 'obj' in args:
                inline_has_add_permission = inline.has_add_permission(request, obj)
            else:
                inline_has_add_permission = inline.has_add_permission(request)

Looks like this should be applied to the other call sites.

#8936 -- Added a view permission and a read-only admin made the addition of the offending can_add = self.has_add_permission(request, obj) if request else True call raising the exception here.

comment:2 Changed 22 months ago by Carlton Gibson

Summary: Backwards-incompatible change of has_add_permission in 2.1Backwards-incompatible change of InlineModelAdmin.has_add_permission() in 2.1

comment:3 Changed 22 months ago by Tim Graham

Owner: changed from nobody to Tim Graham
Status: newassigned

comment:4 Changed 22 months ago by Tim Graham

Has patch: set
Summary: Backwards-incompatible change of InlineModelAdmin.has_add_permission() in 2.1Admin crashes if InlineModelAdmin.has_add_permission() doesn't accept the obj argument

comment:5 Changed 22 months ago by Carlton Gibson

Triage Stage: AcceptedReady for checkin

comment:6 Changed 22 months ago by Carlton Gibson <carlton.gibson@…>

Resolution: fixed
Status: assignedclosed

In fd8a7a5:

Fixed #29723 -- Fixed crash if InlineModelAdmin.has_add_permission() doesn't accept the obj argument.

  • Refs #27991 -- Added testing for ModelAdmin.get_inline_instances() if the inline's has_add_permission() doesn't accept 'obj'.
  • Fixed #29723 -- Fixed crash if InlineModelAdmin.has_add_permission() doesn't accept the obj argument.

comment:7 Changed 22 months ago by Carlton Gibson <carlton.gibson@…>

In 152b1d78:

[2.1.x] Fixed #29723 -- Fixed crash if InlineModelAdmin.has_add_permission() doesn't accept the obj argument.

  • Refs #27991 -- Added testing for ModelAdmin.get_inline_instances() if the inline's has_add_permission() doesn't accept 'obj'.
  • Fixed #29723 -- Fixed crash if InlineModelAdmin.has_add_permission() doesn't accept the obj argument.

Backport of fd8a7a5313f5e223212085b2e470e43c0047e066 from master

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