summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--django/contrib/admin/options.py3
-rw-r--r--docs/ref/contrib/admin/index.txt4
-rw-r--r--docs/releases/2.1.6.txt4
-rw-r--r--tests/modeladmin/tests.py24
4 files changed, 32 insertions, 3 deletions
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
index bcf2a191fe..c6f42d658e 100644
--- a/django/contrib/admin/options.py
+++ b/django/contrib/admin/options.py
@@ -2127,7 +2127,8 @@ class InlineModelAdmin(BaseModelAdmin):
queryset = queryset.none()
return queryset
- def has_add_permission(self, request, obj):
+ def has_add_permission(self, request, obj=None):
+ # RemovedInDjango31Warning: obj becomes a mandatory argument.
if self.opts.auto_created:
# We're checking the rights to an auto-created intermediate model,
# which doesn't have its own individual permissions. The user needs
diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
index bd5547449f..d99552fb9a 100644
--- a/docs/ref/contrib/admin/index.txt
+++ b/docs/ref/contrib/admin/index.txt
@@ -2417,7 +2417,9 @@ The ``InlineModelAdmin`` class adds or customizes:
.. versionchanged:: 2.1
- The ``obj`` argument was added.
+ The ``obj`` argument was added. During the deprecation period, it may
+ also be ``None`` if third-party calls to ``has_add_permission()`` don't
+ provide it.
.. method:: InlineModelAdmin.has_change_permission(request, obj=None)
diff --git a/docs/releases/2.1.6.txt b/docs/releases/2.1.6.txt
index 894eb160b2..d5f342f546 100644
--- a/docs/releases/2.1.6.txt
+++ b/docs/releases/2.1.6.txt
@@ -9,4 +9,6 @@ Django 2.1.6 several bugs in 2.1.5.
Bugfixes
========
-* ...
+* Made the ``obj`` argument of ``InlineModelAdmin.has_add_permission()``
+ optional to restore backwards compatibility with third-party code that
+ doesn't provide it (:ticket:`30097`).
diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py
index de216cbb11..d5a03b1a17 100644
--- a/tests/modeladmin/tests.py
+++ b/tests/modeladmin/tests.py
@@ -712,6 +712,10 @@ class ModelAdminPermissionTests(SimpleTestCase):
def has_perm(self, perm):
return perm == 'modeladmin.add_band'
+ class MockAddUserWithInline(MockUser):
+ def has_perm(self, perm):
+ return perm == 'modeladmin.add_concert'
+
class MockChangeUser(MockUser):
def has_perm(self, perm):
return perm == 'modeladmin.change_band'
@@ -771,6 +775,26 @@ class ModelAdminPermissionTests(SimpleTestCase):
self.assertEqual(len(inline_instances), 1)
self.assertIsInstance(inline_instances[0], ConcertInline)
+ def test_inline_has_add_permission_without_obj(self):
+ # This test will be removed in Django 3.1 when `obj` becomes a required
+ # argument of has_add_permission() (#27991).
+ class ConcertInline(TabularInline):
+ model = Concert
+
+ def has_add_permission(self, request):
+ return super().has_add_permission(request)
+
+ class BandAdmin(ModelAdmin):
+ inlines = [ConcertInline]
+
+ ma = BandAdmin(Band, AdminSite())
+ request = MockRequest()
+ request.user = self.MockAddUserWithInline()
+ band = Band(name='The Doors', bio='', sign_date=date(1965, 1, 1))
+ inline_instances = ma.get_inline_instances(request, band)
+ self.assertEqual(len(inline_instances), 1)
+ self.assertIsInstance(inline_instances[0], ConcertInline)
+
def test_has_change_permission(self):
"""
has_change_permission returns True for users who can edit objects and