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

Fixed #35384 -- Raised FieldError when saving a file without a name to FileField. #18109

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

Conversation

jonnythebard
Copy link
Contributor

@jonnythebard jonnythebard commented Apr 28, 2024

…nce to File field

Trac ticket number

ticket-35384

Branch description

Added code for raising FieldError when trying to save ContentFile instance to File field.

  • The issue originates from pre_save of FileField class.
    • when pre_save get's called to save file-like objects, it checks if file is truthy.
      image
    • File truthy is determined by the following
      image
    • When using regular File or ImageFile instance, even if they do not have explicit name, name would be retrieved from there actual file. But ContentFile instance has nowhere to get its name and its name is set to None.
    • Thus, It cannot pass the if statement(if file and not file._committed:) which results in pre_save method to silently drop it's duty.
  • I suppose pre_save is adequate location to validate if ContentFile.name because looks like it is responsible for letting the file objects to be passed to file storage manager.
  • Simply adding if file.name is None: would affect many other functioning codes.
    • File fields with null=True option will fall into this statement as well
  • Besides None, it is Needed something to represent a ContentFile.name is empty.
  • I added EmptyName class and made it default if ContentFile.name is not passed.
  • The following at pre_save works fine.
    image

Thanks to @john-parton for a good description and isolated tests.

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.
  • I have attached screenshots in both light and dark modes for any UI changes.

@jonnythebard jonnythebard force-pushed the ticket-35384 branch 3 times, most recently from ed13c1f to f5ff4a8 Compare April 28, 2024 02:54
@sarahboyce
Copy link
Contributor

This will also need a release note 👍

@jonnythebard
Copy link
Contributor Author

This will also need a release note 👍

Could you provide me with the guide on how to write release note?

@sarahboyce
Copy link
Contributor

This will also need a release note 👍

Could you provide me with the guide on how to write release note?

Actually as this is a bug fix, we won't need a release note here. So don't worry 😁

@jacobtylerwalls
Copy link
Member

👋 @jonnythebard spotted a typo in the PR title, would you mind fixing?

@jonnythebard jonnythebard changed the title Fixed #35384 -- raise FieldError when trying to save CotnentFile insta… Fixed #35384 -- raise FieldError when trying to save ConTentFile insta… May 5, 2024
@jonnythebard jonnythebard changed the title Fixed #35384 -- raise FieldError when trying to save ConTentFile insta… Fixed #35384 -- raise FieldError when trying to save ContentFile insta… May 5, 2024
@jonnythebard
Copy link
Contributor Author

It looks like part of the fiddly part is that FieldFile.delete would set name to None in order to delete when saving.

I added a test for deleting the field filed to check if any error occurs.

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.

A couple smaller improvements. Looking good!

django/db/models/fields/files.py Outdated Show resolved Hide resolved
tests/model_fields/test_filefield.py Outdated Show resolved Hide resolved
@jonnythebard
Copy link
Contributor Author

Thanks for the comments🙏 @adamchainz

@sarahboyce sarahboyce force-pushed the ticket-35384 branch 2 times, most recently from bd3224e to 71e97a4 Compare May 10, 2024 12:45
@sarahboyce sarahboyce changed the title Fixed #35384 -- raise FieldError when trying to save ContentFile insta… Fixed #35384 -- Raised FieldError when saving a file without a name to FileField. May 10, 2024
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.

Thank you @jonnythebard 👍
I've pushed minor updates. I will give the other a chance to make comments on these before I merge

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