Skip to content

Commit

Permalink
Use model meta to generate verbose name
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dtcooper committed Apr 2, 2024
1 parent 5ed6426 commit 290cafc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
5 changes: 2 additions & 3 deletions admin_confirm/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion admin_confirm/templates/admin/change_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</tr>
{% for field, values in changed_data.items %}
<tr>
<td>{% verbose_name obj field %}</td>
<td>{% verbose_name opts field %}</td>
<td>{{ values.0|format_change_data_field_value }}</td>
<td>{{ values.1|format_change_data_field_value }}</td>
</tr>
Expand Down
5 changes: 2 additions & 3 deletions admin_confirm/templatetags/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 290cafc

Please sign in to comment.