file = models.FileField(upload_to=settings.FILE_PATH)
For uploading a file in django models I used the above line. But For uploading multiple file through django admin model what should I do? I found this But this is for forms. Can I use this for models?
Django provides built-in library and methods that help to upload a file to the server. The forms. FileField() method is used to create a file input and submit the file to the server. While working with files, make sure the HTML form tag contains enctype="multipart/form-data" property.
If you want to have multiple files for the same field you would have to write your own field and widget based on the form field you have found otherwise have a separate model for file with a foreign key to your main model and use ModelInline.
models.py
class Page(models.Model):
title = models.CharField(max_length=255)
class PageFile(models.Model):
file = models.ImageField(upload_to=settings.FILE_PATH)
page = models.ForeignKey('Page')
admin.py
class PageFileInline(admin.TabularInline):
model = PageFile
class PageAdmin(admin.ModelAdmin):
inlines = [PageFileInline,]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With