0

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.

Chris Dao
  • 35
  • 5

1 Answers1

0

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
gasman
  • 23,691
  • 1
  • 38
  • 56
  • Hi @gasman, thank you for your helping, but the message is still not changed. I have tried exactly what you suggest. In my Member model.py, I have defined 2 classes "MemberDeleteView" and "MemberModelAdmin" like the sample you gave, but things don't change. – Chris Dao Sep 01 '21 at 13:30
  • OK, then please update the question to include all the relevant code. – gasman Sep 01 '21 at 14:05
  • I have updated the all the relevant code, please take a look. Thank you. – Chris Dao Sep 01 '21 at 16:03
  • Are you calling `modeladmin_register` with your MemberModelAdmin class at any point? – gasman Sep 01 '21 at 17:41
  • Oh wow, I think I got it, I haven't registered the `MemberModelAdmin` anywhere. – Chris Dao Sep 02 '21 at 08:35
  • Updated: after register the Member Model Admin, I'm now can override the message. Thank you so much @Gasman for helping me. – Chris Dao Sep 02 '21 at 08:36