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)
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
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)
Many thanks to @Gasman for helping me.
As per the docs on overriding ModelAdmin views, create a subclass of DeleteView that overrides the
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
class MemberModelAdmin(ModelAdmin) model = Member delete_view_class = MemberDeleteView