I got this error when I tried to upload and save a zip containing multiple files in the FileField of the form. From the conclusion, it seems that the data format that was thrown at the time of the test was bad.
However, I do not understand that it is difficult to reproduce the error because it clears for some reason when I try to reproduce the error after passing through. I was really in trouble ...
When you first get an error,
tests.py
self.zip_error_test = File(open('tdd.zip'))
file_data = {'upload_train_file': uploadedFile }
form = DocumentForm(file_data)
form.is_valid()
I fold it, but it looks like this. This makes django angry. By the way, I was similarly angry with the ZIP that consolidated one file.
I solved it by changing it as below.
tests.py
with open("tdd.zip", mode="rb") as f:
uploadedFile = InMemoryUploadedFile(f, "field", "tdd.zip", "application/x-zip-compressed", 100, None, content_type_extra={})
file_data = {'field': uploadedFile }
form = DocumentForm(request, data, file_data)
self.assertFalse(form.is_valid(), f"error:{form.errors}/ZIP with multiple files was not validated")
Yes. This will work. You did it.
I thought about various things.
I wrote it in the custom validation of forms.py
in the first place, so I wondered if that was the cause.
I wondered if it wasn't good to bring cleaned_data
, so I played with to_python ().
I was wondering if I could save the file with forms.py
.
The result of trying to think what would happen if I actually uploaded it from an HTML form instead of a test suddenly
I passed. </ b>
I thought that this was different from the data received by form, so I compared the types of data received by views and tests. Then
forms.py
def clean_field(self):
print(type(self.cleaned_data['field']))
# views > django.core.files.uploadedfile.InMemoryUploadedFile
# tests > django.forms.widgets.ClearableFileInput
Wow, it ’s different.
Because of this, I changed the upload for the test to the one mentioned above.
This is one case. Thanks to that, I feel that I have become more familiar with Form. It's the so-called complete understanding. It's Kanpeki.
I tried to write this article after solving it, and when I wrote the code that would cause an error again, it cleared for some reason. Seriously a mystery. I wrote that it passes above
tests.py
with open("tdd.zip", mode="rb") as f:
file_data = {'field': f}
form = DocumentForm(request, data, file_data)
self.assertFalse(form.is_valid(), f"error:{form.errors}/ZIP with multiple files was not validated")
This will still pass. What is it? Didn't you pass?
Anything will pass without the mode =" rb "
. Why is that? Did you get an error? It's true. Shinji.
I'm using the code above, thinking that it might not work again if I restart it. If you change the class explicitly, there will be no problem.
So it was a solution to the phenomenon that zip could not be saved for some reason.
Perhaps next week or so, I will summarize the form knowledge I gained in this matter. I expected to have.
Recommended Posts