Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple workaround to ignore FieldDoesNotExist with custom fields #57

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions admin_confirm/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from contextlib import suppress
import functools
from typing import Dict
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 _
Expand Down Expand Up @@ -175,33 +176,37 @@ def _display_for_changed_data(field, initial_value, new_value):
changed_data = {}
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
)
# Ignore custom fields
with suppress(FieldDoesNotExist):
# 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
)
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():
# Ignore custom fields
with suppress(FieldDoesNotExist):

# Since the form considers initial as the value first shown in the form
# It could be incorrect when user hits save, and then hits "No, go back to edit"
obj.refresh_from_db()
# Since the form considers initial as the value first shown in the form
# It could be incorrect when user hits save, and then hits "No, go back to edit"
obj.refresh_from_db()

field_object = model._meta.get_field(name)
initial_value = getattr(obj, name)
field_object = model._meta.get_field(name)
initial_value = getattr(obj, name)

# Note: getattr does not work on ManyToManyFields
if isinstance(field_object, ManyToManyField):
initial_value = field_object.value_from_object(obj)
# Note: getattr does not work on ManyToManyFields
if isinstance(field_object, ManyToManyField):
initial_value = field_object.value_from_object(obj)

if initial_value != new_value:
changed_data[name] = _display_for_changed_data(
field_object, initial_value, new_value
)
if initial_value != new_value:
changed_data[name] = _display_for_changed_data(
field_object, initial_value, new_value
)

return changed_data

Expand Down