mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Removed ModelAdmin.get_formsets() per deprecation timeline; refs #20702.
This commit is contained in:
parent
3b570dbcdb
commit
2c9e95639e
@ -721,47 +721,12 @@ class ModelAdmin(BaseModelAdmin):
|
||||
self.get_changelist_form(request), extra=0,
|
||||
fields=self.list_editable, **defaults)
|
||||
|
||||
def _get_formsets(self, request, obj):
|
||||
"""
|
||||
Helper function that exists to allow the deprecation warning to be
|
||||
executed while this function continues to return a generator.
|
||||
"""
|
||||
for inline in self.get_inline_instances(request, obj):
|
||||
yield inline.get_formset(request, obj)
|
||||
|
||||
def get_formsets(self, request, obj=None):
|
||||
warnings.warn(
|
||||
"ModelAdmin.get_formsets() is deprecated and will be removed in "
|
||||
"Django 1.9. Use ModelAdmin.get_formsets_with_inlines() instead.",
|
||||
RemovedInDjango19Warning, stacklevel=2
|
||||
)
|
||||
return self._get_formsets(request, obj)
|
||||
|
||||
def get_formsets_with_inlines(self, request, obj=None):
|
||||
"""
|
||||
Yields formsets and the corresponding inlines.
|
||||
"""
|
||||
# We call get_formsets() [deprecated] and check if it triggers a
|
||||
# warning. If it does, then it's ours and we can safely ignore it, but
|
||||
# if it doesn't then it has been overridden so we must warn about the
|
||||
# deprecation.
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
formsets = self.get_formsets(request, obj)
|
||||
|
||||
if len(w) != 1 or not issubclass(w[0].category, RemovedInDjango19Warning):
|
||||
warnings.warn(
|
||||
"ModelAdmin.get_formsets() is deprecated and will be removed in "
|
||||
"Django 1.9. Use ModelAdmin.get_formsets_with_inlines() instead.",
|
||||
RemovedInDjango19Warning, stacklevel=2
|
||||
)
|
||||
if formsets:
|
||||
zipped = zip(formsets, self.get_inline_instances(request, None))
|
||||
for formset, inline in zipped:
|
||||
yield formset, inline
|
||||
else:
|
||||
for inline in self.get_inline_instances(request, obj):
|
||||
yield inline.get_formset(request, obj), inline
|
||||
for inline in self.get_inline_instances(request, obj):
|
||||
yield inline.get_formset(request, obj), inline
|
||||
|
||||
def get_paginator(self, request, queryset, per_page, orphans=0, allow_empty_first_page=True):
|
||||
return self.paginator(queryset, per_page, orphans, allow_empty_first_page)
|
||||
|
@ -1514,26 +1514,6 @@ templates used by the :class:`ModelAdmin` views:
|
||||
You may also simply return a custom :class:`~django.forms.ModelForm` class
|
||||
directly.
|
||||
|
||||
.. method:: ModelAdmin.get_formsets(request, obj=None)
|
||||
|
||||
.. deprecated:: 1.7
|
||||
Use :meth:`get_formsets_with_inlines()` instead.
|
||||
|
||||
Yields :class:`InlineModelAdmin`\s for use in admin add and change views.
|
||||
|
||||
For example if you wanted to display a particular inline only in the change
|
||||
view, you could override ``get_formsets`` as follows::
|
||||
|
||||
class MyModelAdmin(admin.ModelAdmin):
|
||||
inlines = [MyInline, SomeOtherInline]
|
||||
|
||||
def get_formsets(self, request, obj=None):
|
||||
for inline in self.get_inline_instances(request, obj):
|
||||
# hide MyInline in the add view
|
||||
if isinstance(inline, MyInline) and obj is None:
|
||||
continue
|
||||
yield inline.get_formset(request, obj)
|
||||
|
||||
.. method:: ModelAdmin.get_formsets_with_inlines(request, obj=None)
|
||||
|
||||
Yields (``FormSet``, :class:`InlineModelAdmin`) pairs for use in admin add
|
||||
|
@ -1,6 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
import warnings
|
||||
|
||||
from django.contrib import admin
|
||||
from django.contrib.admin.sites import AdminSite
|
||||
@ -9,8 +8,7 @@ from django.contrib.contenttypes.admin import GenericTabularInline
|
||||
from django.contrib.contenttypes.forms import generic_inlineformset_factory
|
||||
from django.forms.formsets import DEFAULT_MAX_NUM
|
||||
from django.forms.models import ModelForm
|
||||
from django.test import RequestFactory, TestCase, ignore_warnings, override_settings
|
||||
from django.utils.deprecation import RemovedInDjango19Warning
|
||||
from django.test import RequestFactory, TestCase, override_settings
|
||||
|
||||
# local test models
|
||||
from .admin import MediaInline, MediaPermanentInline, site as admin_site
|
||||
@ -432,49 +430,6 @@ class GenericInlineModelAdminTest(TestCase):
|
||||
form = ma.get_formset(None).form
|
||||
self.assertEqual(form._meta.fields, ['url', 'description'])
|
||||
|
||||
def test_get_formsets_with_inlines(self):
|
||||
"""
|
||||
get_formsets() triggers a deprecation warning when get_formsets is
|
||||
overridden.
|
||||
"""
|
||||
class MediaForm(ModelForm):
|
||||
class Meta:
|
||||
model = Media
|
||||
exclude = ['url']
|
||||
|
||||
class MediaInline(GenericTabularInline):
|
||||
exclude = ['description']
|
||||
form = MediaForm
|
||||
model = Media
|
||||
|
||||
class EpisodeAdmin(admin.ModelAdmin):
|
||||
inlines = [
|
||||
MediaInline
|
||||
]
|
||||
|
||||
def get_formsets(self, request, obj=None):
|
||||
return []
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
ma = EpisodeAdmin(Episode, self.site)
|
||||
list(ma.get_formsets_with_inlines(request))
|
||||
# Verify that the deprecation warning was triggered when get_formsets was called
|
||||
# This verifies that we called that method.
|
||||
self.assertEqual(len(w), 1)
|
||||
self.assertTrue(issubclass(w[0].category, RemovedInDjango19Warning))
|
||||
|
||||
class EpisodeAdmin(admin.ModelAdmin):
|
||||
inlines = [
|
||||
MediaInline
|
||||
]
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
warnings.simplefilter("always")
|
||||
ma = EpisodeAdmin(Episode, self.site)
|
||||
list(ma.get_formsets_with_inlines(request))
|
||||
self.assertEqual(len(w), 0)
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango19Warning)
|
||||
def test_get_formsets_with_inlines_returns_tuples(self):
|
||||
"""
|
||||
Ensure that get_formsets_with_inlines() returns the correct tuples.
|
||||
@ -500,19 +455,3 @@ class GenericInlineModelAdminTest(TestCase):
|
||||
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__)
|
||||
|
||||
class EpisodeAdmin(admin.ModelAdmin):
|
||||
inlines = [
|
||||
AlternateInline, MediaInline
|
||||
]
|
||||
|
||||
def get_formsets(self, request, obj=None):
|
||||
# Override get_formsets to force the usage of get_formsets in
|
||||
# ModelAdmin.get_formsets_with_inlines() then ignore the
|
||||
# warning raised by ModelAdmin.get_formsets_with_inlines()
|
||||
return self._get_formsets(request, obj)
|
||||
|
||||
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__)
|
||||
|
Loading…
Reference in New Issue
Block a user