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

deferred-fields-patch #1064

Closed
wants to merge 0 commits into from
Closed

Conversation

JordanHyatt
Copy link

@JordanHyatt JordanHyatt commented Nov 26, 2022

Provided a patch that allows objects to be deleted when they have deferred fields present

Description

Added a pre_delete signal that checks to see if there are any deferred fields on the instance that are present in the included_fileds

Related Issue

This is related to issue #678

Motivation and Context

This allows users of simple_history to delete objects without having to consider the consequences of deferring fields in their query

How Has This Been Tested?

Added a test that confirms objects with deferred fields can in-fact be deleted now

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have run the pre-commit run command to format and lint.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have added my name and/or github handle to AUTHORS.rst
  • I have added my change to CHANGES.rst
  • All new and existing tests passed.

@codecov
Copy link

codecov bot commented Dec 9, 2022

Codecov Report

Merging #1064 (362e9e9) into master (27b3dbf) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 362e9e9 differs from pull request most recent head 53bf721. Consider uploading reports for the commit 53bf721 to get more accurate results

@@           Coverage Diff           @@
##           master    #1064   +/-   ##
=======================================
  Coverage   97.24%   97.25%           
=======================================
  Files          23       23           
  Lines        1234     1238    +4     
  Branches      200      201    +1     
=======================================
+ Hits         1200     1204    +4     
  Misses         16       16           
  Partials       18       18           
Impacted Files Coverage Δ
simple_history/models.py 96.76% <100.00%> (+0.04%) ⬆️

... and 10 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@JordanHyatt
Copy link
Author

Anything I can do to help get this PR accepted?

field_attrs = {field.attname for field in fields}
deferred_attrs = instance.get_deferred_fields()
# Load all deferred fields that are present in fields_included
for attr in field_attrs.intersection(deferred_attrs):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to load all deferred fields in a single query, as Django loads fields one at a time on access (See https://docs.djangoproject.com/en/5.1/ref/models/querysets/#defer)

Updated code may look something like this:

instance.refresh_from_db(fields=field_attrs.intersection(deferred_attrs))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea, I have implemented this. Hopefully this gets reviewed and accepted soon!

JordanHyatt added a commit to JordanHyatt/django-simple-history that referenced this pull request Sep 16, 2024
@jwaschkau
Copy link

Hi @JordanHyatt,
the force push reverted your changes. I assume that was a mistake?

@jwaschkau
Copy link

New pull request #1393

@JordanHyatt
Copy link
Author

Yeah I originally did it on my forks master branch, but I wanted to work on HistoricOneToOneField so I had to open a new PR which it looks like you found, thanks!

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