Open jamiecash opened 2 months ago
When modifying the model and changing the image, the error does not occur. This appears to be an issue when editing the model but not adding or changing the image.
Here is an example implementing this: https://django-formset.fly.dev/bootstrap/person (it's contained in the supplied demos of this project).
Maybe I didn't understand your question.
The test app includes the create workflow for creating a Person with an image. The issue occurs on the update and delete workflows. update only works if you reattach the image, submitting with the existing image raises the No file was submitted. Check the encoding type on the form error. delete also does the same.
In summary, the error occurs when a model is loaded with an image and a form is displayed with the model instance, the form is updated but the image or file field is left unchanged, and then the form is submitted. The error occurs in validation, prior to the _formvalid method being called
After a lot of debugging.
To stop this from occurring, if you are loading and updating an Image field, you must use the FileField field from formset.fields. This has an overridden clean method. If you use the default field for models.FileField or models.ImageField then the field is incorrectly marked as changed in the form, which causes the error.
Example
from formset.fields import FileField
class SubjectForm(forms.ModelForm):
image = FileField()
class Meta:
model = models.Subject
fields = ['name', 'image']
Note, you don't have to specify the widget as this is specified in FileField.
This may require an update to the documentation to make it clearer.
Now I see what might be the problem. Does your SubjectForm
also inherit from formset.utils.FormMixin
. This mixin is required to patch some methods. Especially the Django FileField
must be handled in a different way, check here for details: https://github.com/jrief/django-formset/blob/e72e58ce8446b1299fb3987879463e573bd66400/formset/utils.py#L168
When using the UploadedFileInput.On creation, uploaded image is stored successfully, however when modifying the model but not changing the image, or when deleting the model, the following validation error is shown: No file was submitted. Check the encoding type on the form.
The form
The model
When debugging, the FormViews form_valid method is not reached.