Search code examples
pythondjangowagtailwagtail-admin

Override Wagtail delete confirmation message


I would like to override the delete message (to make it more informative, like “if you delete, you will lose 5 items belong to your account”).

My idea is whenever someone deletes my “Member”, it will also delete all the items belong to that member, and the confirmation message should provide more information.

I find that the confirmation message comes from a function named “confirmation_message” in wagtail.contrib.modeladmin.views, at DeleteView class. This function will provide the message for Wagtail delete's template.

This is my Member class:

class Member(ClusterableModel):

user = models.OneToOneField(User, on_delete=models.CASCADE)

email_confirmed = models.BooleanField(default=False)
phone = PhoneNumberField(blank=True)
phone_2 = PhoneNumberField(blank=True)
inside_scoop = models.TextField(blank=True)
lifetime_member = models.BooleanField(default=False)
activation_date = models.DateField(null=True, blank=True, default=timezone.now)
deactivation_date = models.DateField(null=True, blank=True)
points_balance = models.IntegerField(default=0)

favorite_properties = models.ManyToManyField(
    PropertyPage, blank=True, related_name="favorite_properties"
)


base_form_class = MemberFormAdmin

def delete(self: object, *args: list, **kwargs: dict) -> None:
    PropertyPage.objects.filter(owner=self.user).delete()
    self.user.delete()
    return super(self.__class__, self).delete(*args, **kwargs)

This is the default confirmation message that comes from Wagtail: this lies in wagtail -> contrib -> modeladmin -> views.py enter image description here

And this is the delete template: this lies in wagtail -> contrib -> modeladmin -> templates → modeladmin → delete.html enter image description here

This is the message for staff in admin portal: enter image description here


UPDATE 1:

Following @Gasman, I have update my Member models.py like this:

from wagtail.contrib.modeladmin.options import ModelAdmin
from wagtail.contrib.modeladmin.views import DeleteView

class MemberDeleteView(DeleteView):
    def confirmation_message(self):
        return "Hello there!"


class MemberModelAdmin(ModelAdmin):
    model = Member
    delete_view_class = MemberDeleteView

I put all of those codes into my Member models.py: enter image description here

Now my models.py looks like this: enter image description here

However, still not work yet.


UPDATE 2: Problem solved

@Gasman has pointed out that after customised the ModelAdmin, we have to register it to the Wagtail (it will not work until I told Wagtail to use my custom "MemberModelAdmin").

So this is how I register my custom model admin to Wagtail:

from wagtail.contrib.modeladmin.options import modeladmin_register

# Now register the Member Model Admin
modeladmin_register(MemberModelAdmin)

This is my Member models.py: enter image description here

And this is the result after registering the custom model admin: enter image description here

Many thanks to @Gasman for helping me.


Solution

  • As per the docs on overriding ModelAdmin views, create a subclass of DeleteView that overrides the confirmation_message method:

    from wagtail.contrib.modeladmin.views import DeleteView
    
    class MemberDeleteView(DeleteView):
        def confirmation_message(self):
            sprocket_count = self.instance.sprockets.count()
            return "This member has %d sprockets. Are you sure you want to delete?" % sprocket_count
    

    Then, in your ModelAdmin config for that model, specify your custom subclass as delete_view_class:

    class MemberModelAdmin(ModelAdmin)
        model = Member
        delete_view_class = MemberDeleteView