2011-08-12 22:14:58 +08:00
|
|
|
from django.contrib import admin
|
2011-06-17 00:35:04 +08:00
|
|
|
from django.contrib.admin.sites import AdminSite
|
2014-03-06 04:19:40 +08:00
|
|
|
from django.contrib.auth.models import User
|
2014-01-22 14:43:33 +08:00
|
|
|
from django.contrib.contenttypes.admin import GenericTabularInline
|
2015-06-19 03:57:08 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2013-02-12 18:22:41 +08:00
|
|
|
from django.forms.formsets import DEFAULT_MAX_NUM
|
2011-08-12 22:14:58 +08:00
|
|
|
from django.forms.models import ModelForm
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import (
|
|
|
|
RequestFactory, SimpleTestCase, TestCase, override_settings,
|
|
|
|
)
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import reverse
|
2008-11-14 03:03:44 +08:00
|
|
|
|
2014-03-06 04:19:40 +08:00
|
|
|
from .admin import MediaInline, MediaPermanentInline, site as admin_site
|
2015-06-19 03:57:08 +08:00
|
|
|
from .models import Category, Episode, EpisodePermanent, Media, PhoneNumber
|
2008-11-14 03:03:44 +08:00
|
|
|
|
2010-12-04 15:28:12 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestDataMixin:
|
2015-02-23 08:53:57 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
2016-02-06 04:56:52 +08:00
|
|
|
cls.superuser = User.objects.create_superuser(username='super', password='secret', email='super@example.com')
|
2015-02-23 08:53:57 +08:00
|
|
|
|
|
|
|
|
2017-12-29 03:50:38 +08:00
|
|
|
@override_settings(ROOT_URLCONF='generic_inline_admin.urls')
|
2015-02-23 08:53:57 +08:00
|
|
|
class GenericAdminViewTest(TestDataMixin, TestCase):
|
2008-11-14 03:03:44 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(self.superuser)
|
2009-12-17 15:27:26 +08:00
|
|
|
|
2008-11-14 22:47:46 +08:00
|
|
|
e = Episode.objects.create(name='This Week in Django')
|
2009-01-16 10:30:22 +08:00
|
|
|
self.episode_pk = e.pk
|
2008-11-14 22:47:46 +08:00
|
|
|
m = Media(content_object=e, url='http://example.com/podcast.mp3')
|
|
|
|
m.save()
|
2009-12-17 15:27:26 +08:00
|
|
|
self.mp3_media_pk = m.pk
|
|
|
|
|
|
|
|
m = Media(content_object=e, url='http://example.com/logo.png')
|
|
|
|
m.save()
|
|
|
|
self.png_media_pk = m.pk
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_basic_add_GET(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
"""
|
|
|
|
A smoke test to ensure GET on the add_view works.
|
|
|
|
"""
|
2015-02-10 00:28:03 +08:00
|
|
|
response = self.client.get(reverse('admin:generic_inline_admin_episode_add'))
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2009-12-17 15:27:26 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_basic_edit_GET(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
"""
|
|
|
|
A smoke test to ensure GET on the change_view works.
|
|
|
|
"""
|
2015-02-10 00:28:03 +08:00
|
|
|
response = self.client.get(
|
|
|
|
reverse('admin:generic_inline_admin_episode_change', args=(self.episode_pk,))
|
|
|
|
)
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2009-12-17 15:27:26 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_basic_add_POST(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
"""
|
|
|
|
A smoke test to ensure POST on add_view works.
|
|
|
|
"""
|
|
|
|
post_data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"name": "This Week in Django",
|
2008-11-14 03:03:44 +08:00
|
|
|
# inline data
|
2012-06-08 00:08:47 +08:00
|
|
|
"generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": "1",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": "0",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": "0",
|
2008-11-14 03:03:44 +08:00
|
|
|
}
|
2015-02-10 00:28:03 +08:00
|
|
|
response = self.client.post(reverse('admin:generic_inline_admin_episode_add'), post_data)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(response.status_code, 302) # redirect somewhere
|
2009-12-17 15:27:26 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_basic_edit_POST(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
"""
|
|
|
|
A smoke test to ensure POST on edit_view works.
|
|
|
|
"""
|
|
|
|
post_data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"name": "This Week in Django",
|
2008-11-14 03:03:44 +08:00
|
|
|
# inline data
|
2012-06-08 00:08:47 +08:00
|
|
|
"generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": "3",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": "2",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": "0",
|
2020-05-01 22:54:15 +08:00
|
|
|
"generic_inline_admin-media-content_type-object_id-0-id": str(self.mp3_media_pk),
|
2012-06-08 00:08:47 +08:00
|
|
|
"generic_inline_admin-media-content_type-object_id-0-url": "http://example.com/podcast.mp3",
|
2020-05-01 22:54:15 +08:00
|
|
|
"generic_inline_admin-media-content_type-object_id-1-id": str(self.png_media_pk),
|
2012-06-08 00:08:47 +08:00
|
|
|
"generic_inline_admin-media-content_type-object_id-1-url": "http://example.com/logo.png",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-2-id": "",
|
|
|
|
"generic_inline_admin-media-content_type-object_id-2-url": "",
|
2008-11-14 03:03:44 +08:00
|
|
|
}
|
2015-02-10 00:28:03 +08:00
|
|
|
url = reverse('admin:generic_inline_admin_episode_change', args=(self.episode_pk,))
|
2009-01-16 10:30:22 +08:00
|
|
|
response = self.client.post(url, post_data)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(response.status_code, 302) # redirect somewhere
|
2009-12-17 15:27:26 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='generic_inline_admin.urls')
|
2015-02-23 08:53:57 +08:00
|
|
|
class GenericInlineAdminParametersTest(TestDataMixin, TestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
factory = RequestFactory()
|
2009-04-19 04:12:53 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(self.superuser)
|
2009-04-23 06:38:14 +08:00
|
|
|
|
|
|
|
def _create_object(self, model):
|
|
|
|
"""
|
|
|
|
Create a model with an attached Media object via GFK. We can't
|
|
|
|
load content via a fixture (since the GenericForeignKey relies on
|
|
|
|
content type IDs, which will vary depending on what other tests
|
|
|
|
have been run), thus we do it here.
|
|
|
|
"""
|
|
|
|
e = model.objects.create(name='This Week in Django')
|
|
|
|
Media.objects.create(content_object=e, url='http://example.com/podcast.mp3')
|
|
|
|
return e
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_no_param(self):
|
2009-04-19 04:12:53 +08:00
|
|
|
"""
|
|
|
|
With one initial form, extra (default) at 3, there should be 4 forms.
|
|
|
|
"""
|
2009-04-23 06:38:14 +08:00
|
|
|
e = self._create_object(Episode)
|
2015-02-10 00:28:03 +08:00
|
|
|
response = self.client.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
|
2009-04-19 04:12:53 +08:00
|
|
|
formset = response.context['inline_admin_formsets'][0].formset
|
|
|
|
self.assertEqual(formset.total_form_count(), 4)
|
|
|
|
self.assertEqual(formset.initial_form_count(), 1)
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_extra_param(self):
|
2009-04-19 04:12:53 +08:00
|
|
|
"""
|
|
|
|
With extra=0, there should be one form.
|
|
|
|
"""
|
2014-03-06 04:19:40 +08:00
|
|
|
class ExtraInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 0
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [ExtraInline]
|
|
|
|
|
|
|
|
e = self._create_object(Episode)
|
2015-02-10 00:28:03 +08:00
|
|
|
request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
|
2014-03-06 04:19:40 +08:00
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
2009-04-19 04:12:53 +08:00
|
|
|
self.assertEqual(formset.total_form_count(), 1)
|
|
|
|
self.assertEqual(formset.initial_form_count(), 1)
|
|
|
|
|
2019-04-14 21:00:48 +08:00
|
|
|
def test_max_num_param(self):
|
2009-04-19 04:12:53 +08:00
|
|
|
"""
|
|
|
|
With extra=5 and max_num=2, there should be only 2 forms.
|
|
|
|
"""
|
2014-03-06 04:19:40 +08:00
|
|
|
class MaxNumInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 5
|
|
|
|
max_num = 2
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [MaxNumInline]
|
|
|
|
|
|
|
|
e = self._create_object(Episode)
|
2015-02-10 00:28:03 +08:00
|
|
|
request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
|
2014-03-06 04:19:40 +08:00
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
2009-04-19 04:12:53 +08:00
|
|
|
self.assertEqual(formset.total_form_count(), 2)
|
|
|
|
self.assertEqual(formset.initial_form_count(), 1)
|
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_min_num_param(self):
|
2014-03-06 04:19:40 +08:00
|
|
|
"""
|
2014-05-16 11:12:32 +08:00
|
|
|
With extra=3 and min_num=2, there should be five forms.
|
2014-03-06 04:19:40 +08:00
|
|
|
"""
|
|
|
|
class MinNumInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 3
|
|
|
|
min_num = 2
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [MinNumInline]
|
|
|
|
|
|
|
|
e = self._create_object(Episode)
|
2015-02-10 00:28:03 +08:00
|
|
|
request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
|
2014-03-06 04:19:40 +08:00
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
2014-05-16 11:12:32 +08:00
|
|
|
self.assertEqual(formset.total_form_count(), 5)
|
2014-03-06 04:19:40 +08:00
|
|
|
self.assertEqual(formset.initial_form_count(), 1)
|
|
|
|
|
2014-09-23 05:29:44 +08:00
|
|
|
def test_get_extra(self):
|
|
|
|
|
|
|
|
class GetExtraInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 4
|
|
|
|
|
|
|
|
def get_extra(self, request, obj):
|
|
|
|
return 2
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [GetExtraInline]
|
|
|
|
e = self._create_object(Episode)
|
2015-02-10 00:28:03 +08:00
|
|
|
request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
|
2014-09-23 05:29:44 +08:00
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
|
|
|
|
|
|
|
self.assertEqual(formset.extra, 2)
|
|
|
|
|
|
|
|
def test_get_min_num(self):
|
|
|
|
|
|
|
|
class GetMinNumInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
min_num = 5
|
|
|
|
|
|
|
|
def get_min_num(self, request, obj):
|
|
|
|
return 2
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [GetMinNumInline]
|
|
|
|
e = self._create_object(Episode)
|
2015-02-10 00:28:03 +08:00
|
|
|
request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
|
2014-09-23 05:29:44 +08:00
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
|
|
|
|
|
|
|
self.assertEqual(formset.min_num, 2)
|
|
|
|
|
|
|
|
def test_get_max_num(self):
|
|
|
|
|
|
|
|
class GetMaxNumInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
extra = 5
|
|
|
|
|
|
|
|
def get_max_num(self, request, obj):
|
|
|
|
return 2
|
|
|
|
|
|
|
|
modeladmin = admin.ModelAdmin(Episode, admin_site)
|
|
|
|
modeladmin.inlines = [GetMaxNumInline]
|
|
|
|
e = self._create_object(Episode)
|
2015-02-10 00:28:03 +08:00
|
|
|
request = self.factory.get(reverse('admin:generic_inline_admin_episode_change', args=(e.pk,)))
|
2014-09-23 05:29:44 +08:00
|
|
|
request.user = User(username='super', is_superuser=True)
|
|
|
|
response = modeladmin.changeform_view(request, object_id=str(e.pk))
|
|
|
|
formset = response.context_data['inline_admin_formsets'][0].formset
|
|
|
|
|
|
|
|
self.assertEqual(formset.max_num, 2)
|
|
|
|
|
2010-01-21 09:40:21 +08:00
|
|
|
|
2016-02-06 04:56:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF='generic_inline_admin.urls')
|
2015-02-23 08:53:57 +08:00
|
|
|
class GenericInlineAdminWithUniqueTogetherTest(TestDataMixin, TestCase):
|
2010-01-21 09:40:21 +08:00
|
|
|
|
|
|
|
def setUp(self):
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(self.superuser)
|
2010-01-21 09:40:21 +08:00
|
|
|
|
2014-07-08 07:08:42 +08:00
|
|
|
def test_add(self):
|
2010-11-14 02:42:31 +08:00
|
|
|
category_id = Category.objects.create(name='male').pk
|
2010-01-21 09:40:21 +08:00
|
|
|
post_data = {
|
2012-06-08 00:08:47 +08:00
|
|
|
"name": "John Doe",
|
2010-01-21 09:40:21 +08:00
|
|
|
# inline data
|
2012-06-08 00:08:47 +08:00
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-TOTAL_FORMS": "1",
|
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-INITIAL_FORMS": "0",
|
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-MAX_NUM_FORMS": "0",
|
2010-01-21 09:40:21 +08:00
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-0-id": "",
|
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-0-phone_number": "555-555-5555",
|
2020-05-01 22:54:15 +08:00
|
|
|
"generic_inline_admin-phonenumber-content_type-object_id-0-category": str(category_id),
|
2010-01-21 09:40:21 +08:00
|
|
|
}
|
2015-02-10 00:28:03 +08:00
|
|
|
response = self.client.get(reverse('admin:generic_inline_admin_contact_add'))
|
2010-12-04 15:28:12 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2015-02-10 00:28:03 +08:00
|
|
|
response = self.client.post(reverse('admin:generic_inline_admin_contact_add'), post_data)
|
2013-11-03 05:02:56 +08:00
|
|
|
self.assertEqual(response.status_code, 302) # redirect somewhere
|
2010-02-24 01:14:50 +08:00
|
|
|
|
2015-06-19 03:57:08 +08:00
|
|
|
def test_delete(self):
|
|
|
|
from .models import Contact
|
|
|
|
c = Contact.objects.create(name='foo')
|
|
|
|
PhoneNumber.objects.create(
|
|
|
|
object_id=c.id,
|
|
|
|
content_type=ContentType.objects.get_for_model(Contact),
|
|
|
|
phone_number="555-555-5555",
|
|
|
|
)
|
|
|
|
response = self.client.post(reverse('admin:generic_inline_admin_contact_delete', args=[c.pk]))
|
|
|
|
self.assertContains(response, 'Are you sure you want to delete')
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2016-02-08 22:52:39 +08:00
|
|
|
@override_settings(ROOT_URLCONF='generic_inline_admin.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class NoInlineDeletionTest(SimpleTestCase):
|
2011-09-21 02:16:49 +08:00
|
|
|
|
2010-02-24 01:14:50 +08:00
|
|
|
def test_no_deletion(self):
|
2014-07-25 20:07:04 +08:00
|
|
|
inline = MediaPermanentInline(EpisodePermanent, admin_site)
|
2010-02-24 01:14:50 +08:00
|
|
|
fake_request = object()
|
|
|
|
formset = inline.get_formset(fake_request)
|
|
|
|
self.assertFalse(formset.can_delete)
|
2011-06-17 00:35:04 +08:00
|
|
|
|
2011-10-07 08:41:25 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MockRequest:
|
2011-10-07 08:41:25 +08:00
|
|
|
pass
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MockSuperUser:
|
2020-09-14 18:28:17 +08:00
|
|
|
def has_perm(self, perm, obj=None):
|
2011-10-07 08:41:25 +08:00
|
|
|
return True
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2011-10-07 08:41:25 +08:00
|
|
|
request = MockRequest()
|
|
|
|
request.user = MockSuperUser()
|
|
|
|
|
|
|
|
|
2016-02-08 22:52:39 +08:00
|
|
|
@override_settings(ROOT_URLCONF='generic_inline_admin.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class GenericInlineModelAdminTest(SimpleTestCase):
|
2011-06-17 00:35:04 +08:00
|
|
|
|
2011-08-12 22:14:58 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.site = AdminSite()
|
|
|
|
|
2011-06-17 00:35:04 +08:00
|
|
|
def test_get_formset_kwargs(self):
|
|
|
|
media_inline = MediaInline(Media, AdminSite())
|
|
|
|
|
|
|
|
# Create a formset with default arguments
|
2011-10-07 08:41:25 +08:00
|
|
|
formset = media_inline.get_formset(request)
|
2013-02-12 18:22:41 +08:00
|
|
|
self.assertEqual(formset.max_num, DEFAULT_MAX_NUM)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(formset.can_order, False)
|
2011-06-17 00:35:04 +08:00
|
|
|
|
|
|
|
# Create a formset with custom keyword arguments
|
2011-10-07 08:41:25 +08:00
|
|
|
formset = media_inline.get_formset(request, max_num=100, can_order=True)
|
2011-06-17 00:35:04 +08:00
|
|
|
self.assertEqual(formset.max_num, 100)
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(formset.can_order, True)
|
2011-08-12 22:14:58 +08:00
|
|
|
|
|
|
|
def test_custom_form_meta_exclude_with_readonly(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The custom ModelForm's `Meta.exclude` is respected when
|
2011-08-12 22:14:58 +08:00
|
|
|
used in conjunction with `GenericInlineModelAdmin.readonly_fields`
|
|
|
|
and when no `ModelAdmin.exclude` is defined.
|
|
|
|
"""
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
exclude = ['url']
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
readonly_fields = ['description']
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline
|
|
|
|
]
|
|
|
|
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
self.assertEqual(
|
2013-09-04 09:01:45 +08:00
|
|
|
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
|
2011-08-12 22:14:58 +08:00
|
|
|
['keywords', 'id', 'DELETE'])
|
|
|
|
|
|
|
|
def test_custom_form_meta_exclude(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The custom ModelForm's `Meta.exclude` is respected by
|
2011-08-12 22:14:58 +08:00
|
|
|
`GenericInlineModelAdmin.get_formset`, and overridden if
|
|
|
|
`ModelAdmin.exclude` or `GenericInlineModelAdmin.exclude` are defined.
|
|
|
|
Refs #15907.
|
|
|
|
"""
|
|
|
|
# First with `GenericInlineModelAdmin` -----------------
|
|
|
|
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
exclude = ['url']
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
exclude = ['description']
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline
|
|
|
|
]
|
|
|
|
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
self.assertEqual(
|
2013-09-04 09:01:45 +08:00
|
|
|
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
|
2011-08-12 22:14:58 +08:00
|
|
|
['url', 'keywords', 'id', 'DELETE'])
|
|
|
|
|
|
|
|
# Then, only with `ModelForm` -----------------
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
MediaInline
|
|
|
|
]
|
|
|
|
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
self.assertEqual(
|
2013-09-04 09:01:45 +08:00
|
|
|
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
|
2011-08-12 22:14:58 +08:00
|
|
|
['description', 'keywords', 'id', 'DELETE'])
|
2013-07-31 13:50:39 +08:00
|
|
|
|
|
|
|
def test_get_fieldsets(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
# get_fieldsets is called when figuring out form fields.
|
2013-07-31 13:50:39 +08:00
|
|
|
# Refs #18681.
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
can_delete = False
|
|
|
|
|
|
|
|
def get_fieldsets(self, request, obj=None):
|
|
|
|
return [(None, {'fields': ['url', 'description']})]
|
|
|
|
|
|
|
|
ma = MediaInline(Media, self.site)
|
|
|
|
form = ma.get_formset(None).form
|
|
|
|
self.assertEqual(form._meta.fields, ['url', 'description'])
|
2013-09-04 09:01:45 +08:00
|
|
|
|
|
|
|
def test_get_formsets_with_inlines_returns_tuples(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
get_formsets_with_inlines() returns the correct tuples.
|
2013-09-04 09:01:45 +08:00
|
|
|
"""
|
|
|
|
class MediaForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Media
|
|
|
|
exclude = ['url']
|
|
|
|
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class AlternateInline(GenericTabularInline):
|
|
|
|
form = MediaForm
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
AlternateInline, MediaInline
|
|
|
|
]
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
inlines = ma.get_inline_instances(request)
|
|
|
|
for (formset, inline), other_inline in zip(ma.get_formsets_with_inlines(request), inlines):
|
|
|
|
self.assertIsInstance(formset, other_inline.get_formset(request).__class__)
|
2019-03-19 23:13:26 +08:00
|
|
|
|
|
|
|
def test_get_inline_instances_override_get_inlines(self):
|
|
|
|
class MediaInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class AlternateInline(GenericTabularInline):
|
|
|
|
model = Media
|
|
|
|
|
|
|
|
class EpisodeAdmin(admin.ModelAdmin):
|
|
|
|
inlines = (AlternateInline, MediaInline)
|
|
|
|
|
|
|
|
def get_inlines(self, request, obj):
|
|
|
|
if hasattr(request, 'name'):
|
|
|
|
if request.name == 'alternate':
|
|
|
|
return self.inlines[:1]
|
|
|
|
elif request.name == 'media':
|
|
|
|
return self.inlines[1:2]
|
|
|
|
return []
|
|
|
|
|
|
|
|
ma = EpisodeAdmin(Episode, self.site)
|
|
|
|
self.assertEqual(ma.get_inlines(request, None), [])
|
|
|
|
self.assertEqual(ma.get_inline_instances(request), [])
|
|
|
|
for name, inline_class in (('alternate', AlternateInline), ('media', MediaInline)):
|
|
|
|
request.name = name
|
|
|
|
self.assertEqual(ma.get_inlines(request, None), (inline_class,)),
|
|
|
|
self.assertEqual(type(ma.get_inline_instances(request)[0]), inline_class)
|