How it works...
Django model forms are created dynamically from models. They provide the specified fields from the model so you don't need to redefine them manually in the form. In the preceding example, we created a model form for the Idea model. When we save the form, the form knows how to save each field in the database, as well as how to upload the files and save them in the media directory.
The upload_to() function in our example is used for saving the image to a specific directory and defining its name such that it wouldn't clash with filenames for other model instances. Each file will be saved under a path such as ideas/2020/01/0422c6fe-b725-4576-8703-e2a9d9270986.jpg, which consists of the year and month of the upload and the primary ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access