From 6666d7221cfaa0fc739e1132c9f2ed1206e7ebce Mon Sep 17 00:00:00 2001 From: "Henri J. Norden" Date: Mon, 22 Jul 2024 18:58:07 +0300 Subject: [PATCH] Simple workaround to ignore FieldDoesNotExist with custom fields --- admin_confirm/admin.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/admin_confirm/admin.py b/admin_confirm/admin.py index 74ccaf1..c301cf7 100644 --- a/admin_confirm/admin.py +++ b/admin_confirm/admin.py @@ -3,7 +3,7 @@ from django.contrib.admin.exceptions import DisallowedModelAdminToField from django.contrib.admin.utils import flatten_fieldsets, unquote from django.core.cache import cache -from django.core.exceptions import PermissionDenied +from django.core.exceptions import PermissionDenied, FieldDoesNotExist from django.template.response import TemplateResponse from django.contrib.admin.options import TO_FIELD_VAR from django.utils.translation import gettext as _ @@ -176,13 +176,17 @@ def _display_for_changed_data(field, initial_value, new_value): if add: for name, new_value in form.cleaned_data.items(): # Don't consider default values as changed for adding - field_object = model._meta.get_field(name) - default_value = field_object.get_default() - if new_value is not None and new_value != default_value: - # Show what the default value is - changed_data[name] = _display_for_changed_data( - field_object, default_value, new_value - ) + try: + field_object = model._meta.get_field(name) + default_value = field_object.get_default() + if new_value is not None and new_value != default_value: + # Show what the default value is + changed_data[name] = _display_for_changed_data( + field_object, default_value, new_value + ) + except FieldDoesNotExist: + # Ignore custom fields + pass else: # Parse the changed data - Note that using form.changed_data would not work because initial is not set for name, new_value in form.cleaned_data.items():