diff --git a/django_reverse_admin/__init__.py b/django_reverse_admin/__init__.py index 6e9a3bf..63961be 100644 --- a/django_reverse_admin/__init__.py +++ b/django_reverse_admin/__init__.py @@ -310,7 +310,7 @@ def _changeform_view(self, request, object_id, form_url, extra_context): readonly_fields = self.get_readonly_fields(request, obj) adminForm = helpers.AdminForm(form, - list(self.get_fieldsets(request)), + list(self.get_fieldsets(request, obj)), self.prepopulated_fields, readonly_fields=readonly_fields, model_admin=self diff --git a/tests/polls/tests/test_admin.py b/tests/polls/tests/test_admin.py index 8326ce6..55a540d 100644 --- a/tests/polls/tests/test_admin.py +++ b/tests/polls/tests/test_admin.py @@ -1,7 +1,10 @@ +from unittest import mock + from django.contrib.auth.models import User from django.test import TestCase from django.test import Client from django.urls import reverse +from django.core.handlers.wsgi import WSGIRequest from polls.models import Address from polls.models import AddressNonId @@ -10,7 +13,7 @@ from polls.models import PersonWithTwoAddresses from polls.admin import SITE_HEADER import polls.tests.config as test_config - +from django_reverse_admin import ReverseModelAdmin class AddressAdminTest(TestCase): def setUp(self): @@ -137,3 +140,20 @@ def test_admin_header(self): change_url = reverse('admin:polls_personwithtwoaddresses_add') res = client.get(change_url) self.assertTrue(SITE_HEADER in str(res.content), 'Custom header is set') + + @mock.patch.object(ReverseModelAdmin, 'get_fieldsets') + def test_called_args(self, mock): + person = PersonWithAddressNonId.objects.create(name='Toto', home_addr=AddressNonId.objects.create()) + + client = Client() + client.login(**test_config.ADMIN_USER) + change_url = reverse('admin:polls_personwithaddressnonid_change', args=(person.pk,)) + # Ensure it doesn't raise any exception. + client.get(change_url, test_config.PERSON_WITH_TWO_ADDRESSES) + self.assertEqual(mock.call_count, 2) + self.assertEqual(2, len(mock.call_args_list[0].args)) + self.assertIsInstance(mock.call_args_list[0].args[0], WSGIRequest) + self.assertIsInstance(mock.call_args_list[0].args[1], PersonWithAddressNonId) + self.assertEqual(2, len(mock.call_args_list[1].args)) + self.assertIsInstance(mock.call_args_list[1].args[0], WSGIRequest) + self.assertIsInstance(mock.call_args_list[1].args[1], PersonWithAddressNonId)