Avoid exception in changes_display_dict
when model is missing
#609
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As django-auditlog v3 now stores the actual PKs for foreign models instead of their string representation, I switched my application from
changes_dict
tochanges_display_dict
so that the actual string-representation of foreign models is being shown again. While this worked in my local environment, it broke while trying out this change against a previously used testing database, specifically by raising aKeyError
.As it turns out, if a model has been previously registered with django-auditlog and made any entries, followed by then being no longer registered (e.g. if the model has been deprecated or further logging is no longer desired), any calls to
changes_display_dict
will fail. The reason behind this is that the method attempts to lookup the model within the registry, without checking if it actually still exists.This lookup is only needed for respecting
mapping_fields
and has no other purpose. This PR extends the logic to first check if the model is contained in the registry, and only if that is the case, the actual lookup happens. If a previously existing model is being looked up instead, it will still show up properly, just without the mapped fields as they no longer exist.