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
Writing null or empty string to FileField does not empty the reference #937
Comments
Not really by design no, not sure if this needs addressing or not. |
Duplicated by #949. Let's follow up against that ticket. |
Not sure if this is a dup or not afterall, will reopen for now. |
I've run into this issue as well. When using I think the fix is to fall back on Does that make sense? |
Fixed via #1003. |
I was trying out the new version that has this change and noticed my ImageFields were being inadvertently emptied out. I totally understand the reasoning behind this change but I think it may have had unintended consequences. For example, if you are viewing a specific record through the browsable API and click the PUT button on the HTML form at the bottom, you've now destroyed data without even thinking about it, because the FileField is rendered without a file selected, so it gets emptied out when the form is submitted. I'm not sure if there's a better way of going about this or not, just wanted to share my experience. I definitely like the idea of having a built in way to clear out these fields so I can get rid of my custom code to handle that in pre_save. |
Interesting. I wonder if we can pre-fill the form with the filename, to avoid it from 'changing'? Just a thought... note that I haven't thought about this problem in a few months now... |
Hmmm possibly, I can't figure out a way to get it to work though. I think browser security restrictions may prevent us from going down that route. I can't think of a way to do this without using magical fields that don't actually exist on the model. There's gotta be some clever solution out there! |
Anybody have any other thoughts on this one? |
@tomchristie Any thoughts on this? It seems odd that you can no longer use the browsable API to update a record that has a FileField on it without having to upload the same file every time. |
@fletchowns I don't really have much spare time to look into it right now - it'd be great if someone else was able to take ownership of investigating the issue and pushing through a fix. |
Hi, I hit this issue in my code and it seems like a real problem: it basically makes PUT requests unusable on views containing a FileField/ImageField. Is it reasonable to expect that sending a PUT request with the content of a GET request on the same resource should have no effect ? If this is the case, the problem comes from the asymmetry between I'm not very familiar with the codebase yet. Do you think this is a good solution ? And is it possible to implement ? |
The semantics of a null value are equally ambiguous, in my opinion. Perhaps -W On Tue, Apr 22, 2014 at 8:10 AM, Martin Maillard
|
We're using PATCH with FileFields/ImageFields etc, so need a way of clearing the individual field. On our frontend the files are objects, so more natural to express empty state as null rather than an empty string. Here's our hack, applied to our base serializer --
|
I verified, this works. However, I also found that another solution which might be a bit better. Copy from the source, in serializers add "allow_null=True" to the ImageField / FileField: class PartSerializer(serializers.ModelSerializer):
image = serializers.ImageField(max_length=None, allow_empty_file=True, allow_null=True, required=False)
class Meta:
model = Part
fields = ('id', 'image') |
It does not seem possible to use PATCH on a FileField to null out the field. Is this by design?
The text was updated successfully, but these errors were encountered: