From 290cafc7ba76b46b09e530fce205bb023b4fc4f7 Mon Sep 17 00:00:00 2001 From: David Cooper Date: Tue, 2 Apr 2024 09:48:46 -0400 Subject: [PATCH] Use model meta to generate verbose name Render field names of object rather than the word "None" This is fix for issue #53, since an object may not exist when rendering `templates/admin/change_data.html` - ie, if we're greating a new object when `confirm_add = True` is set on a `ModelAdmin` with the `AdminConfirmMixin`. Also makes sure we send an actual object (not the string "None") to the base template, to remove other instances of the word "None" from the add view. --- admin_confirm/admin.py | 5 ++--- admin_confirm/templates/admin/change_data.html | 2 +- admin_confirm/templatetags/formatting.py | 5 ++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/admin_confirm/admin.py b/admin_confirm/admin.py index 74ccaf1..3914c51 100644 --- a/admin_confirm/admin.py +++ b/admin_confirm/admin.py @@ -423,13 +423,12 @@ def _change_confirmation_view(self, request, object_id, form_url, extra_context) **self.admin_site.each_context(request), "preserved_filters": self.get_preserved_filters(request), "title": f"{_('Confirm')} {title_action} {opts.verbose_name}", - "subtitle": str(obj), - "object_name": str(obj), + "subtitle": obj and str(obj), + "object_name": obj and str(obj), "object_id": object_id, "app_label": opts.app_label, "model_name": opts.model_name, "opts": opts, - "obj": obj, "changed_data": changed_data, "add": add, "save_as_new": SAVE_AS_NEW in request.POST, diff --git a/admin_confirm/templates/admin/change_data.html b/admin_confirm/templates/admin/change_data.html index c9c3ebc..d781be8 100644 --- a/admin_confirm/templates/admin/change_data.html +++ b/admin_confirm/templates/admin/change_data.html @@ -9,7 +9,7 @@ {% for field, values in changed_data.items %} - {% verbose_name obj field %} + {% verbose_name opts field %} {{ values.0|format_change_data_field_value }} {{ values.1|format_change_data_field_value }} diff --git a/admin_confirm/templatetags/formatting.py b/admin_confirm/templatetags/formatting.py index 499be63..7c3c28a 100644 --- a/admin_confirm/templatetags/formatting.py +++ b/admin_confirm/templatetags/formatting.py @@ -20,6 +20,5 @@ def format_change_data_field_value(field_value): @register.simple_tag -def verbose_name(obj, fieldname): - if obj: - return obj._meta.get_field(fieldname).verbose_name +def verbose_name(opts, fieldname): + return opts.get_field(fieldname).verbose_name.capitalize()