Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload multiple file in django admin models

Tags:

python

django

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?

like image 876
user12345 Avatar asked Dec 03 '10 07:12

user12345


People also ask

How do I upload files to Django project?

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.


1 Answers

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,]
like image 160
sunn0 Avatar answered Oct 13 '22 20:10

sunn0