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

Avoid exception in changes_display_dict when model is missing #609

Merged
merged 1 commit into from
Feb 13, 2024

Conversation

ppmathis
Copy link
Contributor

As django-auditlog v3 now stores the actual PKs for foreign models instead of their string representation, I switched my application from changes_dict to changes_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 a KeyError.

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.

Copy link

codecov bot commented Feb 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d78f813) 95.20% compared to head (fa0fbba) 95.21%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #609      +/-   ##
==========================================
+ Coverage   95.20%   95.21%   +0.01%     
==========================================
  Files          31       31              
  Lines        1021     1025       +4     
==========================================
+ Hits          972      976       +4     
  Misses         49       49              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hramezani
Copy link
Member

Thanks @ppmathis

@hramezani hramezani merged commit e6fc810 into jazzband:master Feb 13, 2024
8 checks passed
@ppmathis ppmathis deleted the handle-missing-models branch February 14, 2024 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants