user12345
user12345

Reputation: 2418

How to upload multiple file in django admin models

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?

Upvotes: 6

Views: 4425

Answers (1)

sunn0
sunn0

Reputation: 3046

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,]

Upvotes: 5

Related Questions