My simplified setup:
Model
class myFile(models.Model):
filename = models.CharField(_('file name'), max_length=255, null=False, blank=False, default=_('file name'))
status = models.CharField(_('status'), max_length=255, null=False, blank=False, default=_('Draft'))
file = models.FileField(null=True, max_length=300)
Form
class myFileCreationForm(forms.ModelForm):
class Meta:
model = myFile
fields = ['file', 'status', 'filename', ]
widgets = {
'file': forms.ClearableFileInput(attrs={'class': 'file-select'}),
'status': forms.TextInput(attrs={'class': 'input is-primary'}),
'filename': forms.TextInput(attrs={'class': 'input is-primary'}),
}
def __init__(self, *args, **kwargs):
super(myFileCreationForm, self).__init__(*args, **kwargs)
self.fields['status'].initial = "Draft"
Now I would like to test the form
TestCase
class TestCase(TestCase):
def setUp(self):
pass
def test_create_file_form(self):
file_data = {"testdocument": SimpleUploadedFile("face.doc", b"file data")}
post_data = {"filename": "face.doc", "status": "ISSUE"}
form = myFileCreationForm(data=post_data, files=file_data)
print(f"Form erros: {form.errors}" )
assert form.is_valid()
According to:
This should be the right setup BUT 'form.errors give me
Form errors: <ul class="errorlist"><li>file<ul class="errorlist"><li>This field is required.</li></ul></li></ul>
‘file’ is required ?
My template form is as it should be.
<form action="" method="post" enctype="multipart/form-data" >
I do not understand what is wrong here, or how to debug this.
Any suggestions?