Ticket #11250: admin_can_delete.diff

File admin_can_delete.diff, 4.9 KB (added by gsf, 6 years ago)
  • django/contrib/admin/options.py

     
    11091109    fk_name = None
    11101110    formset = BaseInlineFormSet
    11111111    extra = 3
     1112    can_delete = True
    11121113    max_num = 0
    11131114    template = None
    11141115    verbose_name = None
     
    11541155            "exclude": (exclude + kwargs.get("exclude", [])) or None,
    11551156            "formfield_callback": curry(self.formfield_for_dbfield, request=request),
    11561157            "extra": self.extra,
     1158            "can_delete": self.can_delete,
    11571159            "max_num": self.max_num,
    11581160        }
    11591161        defaults.update(kwargs)
  • django/contrib/contenttypes/generic.py

     
    386386            "formfield_callback": self.formfield_for_dbfield,
    387387            "formset": self.formset,
    388388            "extra": self.extra,
    389             "can_delete": True,
     389            "can_delete": self.can_delete,
    390390            "can_order": False,
    391391            "fields": fields,
    392392            "max_num": self.max_num,
  • tests/regressiontests/generic_inline_admin/tests.py

     
    55from django.contrib.contenttypes.generic import generic_inlineformset_factory
    66
    77# local test models
    8 from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, Media
     8from models import Episode, EpisodeExtra, EpisodeCanDelete, EpisodeMaxNum, EpisodeExclude, Media
    99
    1010class GenericAdminViewTest(TestCase):
    1111    fixtures = ['users.xml']
     
    121121        self.assertEqual(formset.total_form_count(), 1)
    122122        self.assertEqual(formset.initial_form_count(), 1)
    123123
     124    def testCanDeleteParam(self):
     125        """
     126        With can_delete=False, there should be no ability to delete.
     127        """
     128        e = self._create_object(EpisodeCanDelete)
     129        response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodecandelete/%s/' % e.pk)
     130        formset = response.context['inline_admin_formsets'][0].formset
     131        self.assertEqual(formset.can_delete, False)
     132
    124133    def testMaxNumParam(self):
    125134        """
    126135        With extra=5 and max_num=2, there should be only 2 forms.
     
    139148        e = self._create_object(EpisodeExclude)
    140149        response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/%s/' % e.pk)
    141150        formset = response.context['inline_admin_formsets'][0].formset
    142         self.failIf('url' in formset.forms[0], 'The formset has excluded "url" field.')
     151        self.failIf('url' in formset.forms[0], 'The formset has excluded "url" field.')
     152
  • tests/regressiontests/generic_inline_admin/models.py

     
    4646admin.site.register(EpisodeExtra, inlines=[MediaExtraInline])
    4747
    4848#
     49# Generic inline with can_delete = True
     50#
     51
     52class EpisodeCanDelete(Episode):
     53    pass
     54
     55class MediaCanDeleteInline(generic.GenericTabularInline):
     56    model = Media
     57    can_delete = False
     58
     59admin.site.register(EpisodeCanDelete, inlines=[MediaCanDeleteInline])
     60
     61#
    4962# Generic inline with extra and max_num
    5063#
    5164
  • tests/regressiontests/modeladmin/models.py

     
    908908...     inlines = [ValidationTestInline]
    909909>>> validate(ValidationTestModelAdmin, ValidationTestModel)
    910910
     911# can_delete
     912
     913>>> class ValidationTestInline(TabularInline):
     914...     model = ValidationTestInlineModel
     915...     can_delete = False
     916>>> class ValidationTestModelAdmin(ModelAdmin):
     917...     inlines = [ValidationTestInline]
     918>>> validate(ValidationTestModelAdmin, ValidationTestModel)
     919
    911920# max_num
    912921
    913922>>> class ValidationTestInline(TabularInline):
  • docs/ref/contrib/admin/index.txt

     
    987987to the initial forms. See the
    988988:ref:`formsets documentation <topics-forms-formsets>` for more information.
    989989
     990``can_delete``
     991~~~~~~~~~~~~~~
     992
     993The value for ``can_delete`` defaults to ``True``.  This controls the ability
     994to delete relations in the formset. See the
     995:ref:`formsets documentation <topics-forms-formsets>` for more information.
     996
    990997``max_num``
    991998~~~~~~~~~~~
    992999
Back to Top