Ticket #11108: delete_model.diff

File delete_model.diff, 6.4 KB (added by apollo13, 4 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index ffc6e79..caaf09d 100644
    a b class ModelAdmin(BaseModelAdmin): 
    601601        """
    602602        obj.save()
    603603
     604    def delete_model(self, requet, obj):
     605        """
     606        Given a model instance delete it from the database.
     607        """
     608        obj.delete()
     609
    604610    def save_formset(self, request, form, formset, change):
    605611        """
    606612        Given an inline formset save it to the database.
    class ModelAdmin(BaseModelAdmin): 
    11221128                raise PermissionDenied
    11231129            obj_display = force_unicode(obj)
    11241130            self.log_deletion(request, obj, obj_display)
    1125             obj.delete()
     1131            self.delete_model(request, obj)
    11261132
    11271133            self.message_user(request, _('The %(name)s "%(obj)s" was deleted successfully.') % {'name': force_unicode(opts.verbose_name), 'obj': force_unicode(obj_display)})
    11281134
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index ac517e8..df41ab2 100644
    a b templates used by the :class:`ModelAdmin` views: 
    757757``ModelAdmin`` methods
    758758----------------------
    759759
     760.. warning::
     761
     762    ``save_model`` and ``delete_model`` have to save/delete the object, those
     763    methods are not for veto purposes, they just allow you to do some extra
     764    operations.
     765
    760766.. method:: ModelAdmin.save_model(self, request, obj, form, change)
    761767
    762768The ``save_model`` method is given the ``HttpRequest``, a model instance,
    For example to attach ``request.user`` to the object prior to saving:: 
    770776            obj.user = request.user
    771777            obj.save()
    772778
     779.. method:: ModelAdmin.delete_model(self, request, obj)
     780
     781The ``delete_model`` method is given the ``HttpRequest`` and a model instance.
     782Use this method to to pre- or post-delete operations.
     783
    773784.. method:: ModelAdmin.save_formset(self, request, form, formset, change)
    774785
    775786The ``save_formset`` method is given the ``HttpRequest``, the parent
  • docs/topics/db/multi-db.txt

    diff --git a/docs/topics/db/multi-db.txt b/docs/topics/db/multi-db.txt
    index 1a939b0..0e9290c 100644
    a b database other than that that specified by your router chain, you'll 
    458458need to write custom :class:`~django.contrib.admin.ModelAdmin` classes
    459459that will direct the admin to use a specific database for content.
    460460
    461 ``ModelAdmin`` objects have four methods that require customization for
     461``ModelAdmin`` objects have five methods that require customization for
    462462multiple-database support::
    463463
    464464    class MultiDBModelAdmin(admin.ModelAdmin):
    multiple-database support:: 
    469469            # Tell Django to save objects to the 'other' database.
    470470            obj.save(using=self.using)
    471471
     472        def delete_model(self, requqest, obj):
     473            # Tell Django to delete objects from the 'other' database
     474            obj.delete(using=self.using)
     475
    472476        def queryset(self, request):
    473477            # Tell Django to look for objects on the 'other' database.
    474478            return super(MultiDBModelAdmin, self).queryset(request).using(self.using)
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index b25a9b9..ee0f5d5 100644
    a b class ArticleAdmin(admin.ModelAdmin): 
    107107    modeladmin_year.admin_order_field = 'date'
    108108    modeladmin_year.short_description = None
    109109
     110    def delete_model(self, request, obj):
     111        EmailMessage(
     112            'Greetings from a deleted object',
     113            'I hereby inform you that some user deleted me',
     114            'from@example.com',
     115            ['to@example.com']
     116        ).send()
     117        return super(ArticleAdmin, self).delete_model(request, obj)
     118
     119    def save_model(self, request, obj, form, change=True):
     120        EmailMessage(
     121            'Greetings from a created object',
     122            'I hereby inform you that some user created me',
     123            'from@example.com',
     124            ['to@example.com']
     125        ).send()
     126        return super(ArticleAdmin, self).save_model(request, obj, form, change)
     127
     128
    110129class CustomArticle(models.Model):
    111130    content = models.TextField()
    112131    date = models.DateTimeField()
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 4c96306..a3fe34f 100644
    a b  
    33import re
    44import datetime
    55from django.conf import settings
     6from django.core import mail
    67from django.core.files import temp as tempfile
    78from django.contrib.auth import admin # Register auth models with the admin.
    89from django.contrib.auth.models import User, Permission, UNUSABLE_PASSWORD
    class AdminViewPermissionsTest(TestCase): 
    540541        post = self.client.post('/test_admin/admin/admin_views/article/add/', add_dict)
    541542        self.assertRedirects(post, '/test_admin/admin/')
    542543        self.failUnlessEqual(Article.objects.all().count(), 4)
     544        self.assertEquals(len(mail.outbox), 1)
     545        self.assertEquals(mail.outbox[0].subject, 'Greetings from a created object')
    543546        self.client.get('/test_admin/admin/logout/')
    544547
    545548        # Super can add too, but is redirected to the change list view
    class AdminViewPermissionsTest(TestCase): 
    695698        post = self.client.post('/test_admin/admin/admin_views/article/1/delete/', delete_dict)
    696699        self.assertRedirects(post, '/test_admin/admin/')
    697700        self.failUnlessEqual(Article.objects.all().count(), 2)
     701        self.assertEquals(len(mail.outbox), 1)
     702        self.assertEquals(mail.outbox[0].subject, 'Greetings from a deleted object')
    698703        article_ct = ContentType.objects.get_for_model(Article)
    699704        logged = LogEntry.objects.get(content_type=article_ct, action_flag=DELETION)
    700705        self.failUnlessEqual(logged.object_id, u'1')
    class AdminInheritedInlinesTest(TestCase): 
    14401445        self.failUnlessEqual(BarAccount.objects.all()[0].username, "%s-1" % bar_user)
    14411446        self.failUnlessEqual(Persona.objects.all()[0].accounts.count(), 2)
    14421447
    1443 from django.core import mail
    1444 
    14451448class AdminActionsTest(TestCase):
    14461449    fixtures = ['admin-views-users.xml', 'admin-views-actions.xml']
    14471450
Back to Top