[Django]-How to add a new instance of Django model with FileField by ModelForm?

0👍

As it is now, a file is required. Are you trying to save the form without a file?
If you want to make the file optional, you need to define it in this way:

class Document(models.Model):
    file = models.FileField(upload_to="documents", blank=True, null=True)

As a an additional note, the action parameter you have in the form may be incorrect.
It should be an URL; usually in Django you would want to put ".", but not a completely empty string (""). That said, I do not know if this may be an issue or not.

👤rob

0👍

In you Document model, you have your file set to upload_t0 “documents”, but where exactly is upload_to pointed to.

May be this will help.

👤Milind

Leave a comment