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

Fix #8307 -- Saving a model with an ImageField with width_field or height_field no longer results in an extra read operation #18070

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

john-parton
Copy link
Contributor

@john-parton john-parton commented Apr 12, 2024

Trac ticket number

ticket-8307

Branch description

This is an alternate approach from #17775 to solving 8307.

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • For UI changes, I have attached screenshots in both light and dark modes.

@john-parton john-parton changed the title Bugfix/8307 anti pattern removal Fix #8307 -- Saving a model with an ImageField with width_field or height_field no longer results in an extra read operation [Alternate Proposal] Apr 12, 2024
@john-parton
Copy link
Contributor Author

john-parton commented Apr 15, 2024

Updated docs here.

The reason I moved the logic to the descriptor, is because in order to get the behavior correct, the logic needs both the value before and after the value is set. A previous pull request I had open instead modified the ImageField.update_image_dimensions method, requiring that the initial file be passed in as an additional kwarg. It also required an additional container class to tell the descriptor when to pass the original file.

Should I introduce I setting to opt-in to this new behavior? Or is it safe enough to just update?

Copy link
Contributor

@sarahboyce sarahboyce left a comment

Choose a reason for hiding this comment

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

Hi @john-parton thank you for this ⭐ I have initial comments

  • Close the other PR if this is the preferred approach?
  • I wanted to start reviewing this PR as I know you were waiting for some feedback, can you give an overview on how these two relate https://code.djangoproject.com/ticket/35384?

There are some small stylistic things that we need to update about our Python style. Including ending comments with full stops etc.

docs/ref/models/fields.txt Outdated Show resolved Hide resolved
docs/ref/models/fields.txt Show resolved Hide resolved
tests/model_fields/test_imagefield.py Outdated Show resolved Hide resolved
django/db/models/fields/files.py Outdated Show resolved Hide resolved
django/db/models/fields/files.py Outdated Show resolved Hide resolved
django/db/models/fields/files.py Outdated Show resolved Hide resolved
@john-parton
Copy link
Contributor Author

Hi @john-parton thank you for this ⭐ I have initial comments

* Close the other PR if this is the preferred approach?

Done

* I wanted to start reviewing this PR as I know you were waiting for some feedback, can you give an overview on how these two relate https://code.djangoproject.com/ticket/35384?

https://code.djangoproject.com/ticket/35384 is unrelated technically. It's just another issue I found while digging through the code. This PR doesn't close that ticket.

There are some small stylistic things that we need to update about our Python style. Including ending comments with full stops etc.

Understood

@john-parton john-parton force-pushed the bugfix/8307-anti-pattern-removal branch from 7c07fc7 to b191f75 Compare April 26, 2024 15:44
@john-parton
Copy link
Contributor Author

Rebased and forced pushed to fix conflict with release notes.

@john-parton john-parton changed the title Fix #8307 -- Saving a model with an ImageField with width_field or height_field no longer results in an extra read operation [Alternate Proposal] Fix #8307 -- Saving a model with an ImageField with width_field or height_field no longer results in an extra read operation Apr 26, 2024
@john-parton
Copy link
Contributor Author

@sarahboyce I went ahead and removed all of the changes other than the "core" part of the fix you identified. It seems so obvious now, but it definitely isn't. I adjusted the comment slightly to help guide someone reading that part of the code.

I also removed all of the changes to other comments, so it's just those specific lines.

In referring to the documentation change I made:

Name of a model field which will be auto-populated with the height of the image each time the image attribute is set.

This is not because the behavior changed. This has been the behavior forever, but I wanted it to be clear that the height and width fields do not get updated when the model is saved if the image field isn't changed. That might seem obvious, but if you have a cron job that scans all your images and resizes them, then they won't get the updated sizes.

Copy link
Contributor

@sarahboyce sarahboyce left a comment

Choose a reason for hiding this comment

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

@sarahboyce I went ahead and removed all of the changes other than the "core" part of the fix you identified. It seems so obvious now, but it definitely isn't. I adjusted the comment slightly to help guide someone reading that part of the code.
I also removed all of the changes to other comments, so it's just those specific lines.

Thank you so much this looks great 🥳
I have a few minor comments for us to add the final polish but it's looking really good

This is not because the behavior changed. This has been the behavior forever, but I wanted it to be clear that the height and width fields do not get updated when the model is saved if the image field isn't changed. That might seem obvious, but if you have a cron job that scans all your images and resizes them, then they won't get the updated sizes.

Ok thank you for highlighting. I think, let's pull those couple of lines into a separate PR. The reason being is sometimes document clarifications get back ported (to 5.0 for example) and if this is unrelated to your change, let's pull it out and deal with it separately.

After that, can you squash all commits into the one commit that has a message something like:

Fixed #8307 -- Prevented a read operation when saving ImageFields with width_field and height_field.

I will also get a second opinion on the release notes, docs and the decision not to deprecate.

tests/model_fields/models.py Outdated Show resolved Hide resolved
tests/model_fields/storage.py Outdated Show resolved Hide resolved
tests/model_fields/test_imagefield.py Outdated Show resolved Hide resolved
docs/ref/models/fields.txt Outdated Show resolved Hide resolved
@john-parton
Copy link
Contributor Author

I moved the doc change which wasn't directly related to the bugfix here: #18112

I accepted all of your suggestions, and then rewrote the tests to use the model you suggested. Fixed linter errors, rebased and force-pushed with the commit message you suggested.

@sarahboyce
Copy link
Contributor

sarahboyce commented Apr 29, 2024

Thank you John

I will also get a second opinion on the release notes, docs and the decision not to deprecate.

I have started a discussion here to get feedback on whether we need to deprecate.
For context, I am still quite new to the Fellow role so I'm sorry that I'm a little cautious/unsure about how best to proceed.

The PR looks great! If we do need to add a deprecation path, it shouldn't be too much work 👍

Copy link
Sponsor Member

@adamchainz adamchainz left a comment

Choose a reason for hiding this comment

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

Great work on this old ticket!!

I have some small suggestions.

tests/model_fields/storage.py Outdated Show resolved Hide resolved
tests/model_fields/test_imagefield.py Outdated Show resolved Hide resolved
django/db/models/fields/files.py Outdated Show resolved Hide resolved
@john-parton john-parton force-pushed the bugfix/8307-anti-pattern-removal branch from cc9a5e9 to 767d6f1 Compare April 29, 2024 22:00
@john-parton
Copy link
Contributor Author

  • Merged in suggested changes
  • tweaked tests (as requested) so they continue to pass

Looks like based on our discussion in the forum that most likely we'll want an example of how to restore the old behavior. Again, I know I'm a broken record: I can't imagine why you would WANT to do that, but at least we'll have our bases covered.

@john-parton john-parton force-pushed the bugfix/8307-anti-pattern-removal branch from 767d6f1 to 6dea56e Compare May 1, 2024 22:25
@john-parton
Copy link
Contributor Author

Force pushed because CI failed (error in main or a bad merge? Not sure.)

@sarahboyce
Copy link
Contributor

Force pushed because CI failed (error in main or a bad merge? Not sure.)

No problem, agreed unrelated to your changes 👍

@john-parton
Copy link
Contributor Author

Mostly a note for myself: I mentioned adding docs somewhere to guide users on how to "force" django to re-read the image after it's been persisted to update the width and height fields. Still need to do that.

@adamchainz
Copy link
Sponsor Member

Please squash commits and retitle Fixed #8307 -- Prevented re-read when saving an ImageField. to follow the commit guidelines.

@john-parton john-parton force-pushed the bugfix/8307-anti-pattern-removal branch from 6dea56e to 9c4a122 Compare May 3, 2024 19:36
@john-parton
Copy link
Contributor Author

Rebased, squashed, changed commit message.

In the future, should I go ahead and squash every time I patch? I usually leave the extra commits on in between reviews so that my individual changes can be seen.

@adamchainz
Copy link
Sponsor Member

I always squash every time I change, I think it’s what’s general done on GitHub. Otherwise the merger has to do it for you. The diff can be seen on GitHub by clicking the “Compare” link next to the message “John Parton force-pushed the bugfix/8307-anti-pattern-removal branch from 6dea56e to 9c4a122”.

@john-parton
Copy link
Contributor Author

That's good info. Thanks. I'll make sure to be more religious with my squashing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants