如何从Django管理员上传多个文件? [英] How to upload multiple files from the Django admin?

查看:48
本文介绍了如何从Django管理员上传多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Django管理员中上传多个文件,而不必放置多个FileField字段.用户可以通过一种简单的方式来管理文件;删除或更改每个上传的文件,但一次上传几个.

I want to upload multiple files in the Django admin, without having to place multiple FileField fields. The user can be able to manage the files in a simple way; delete or change each uploaded file but upload several at once.

我认为可行的解决方案是使用多个文件字段,但问题是,我不知道用户将上传多少文件

the solution I see viable is to use several filefield but the problem is, i dont know how many files the user will upload

def case_upload_location(instance, filename):
    case_name = instance.name.lower().replace(" ", "-")
    file_name = filename.lower().replace(" ", "-")
    return "casos/{}/{}".format(case_name, file_name)


class Case(models.Model):
    name            = models.CharField(max_length=250)
    observations    = models.TextField(null = True, blank = True)
    number_folder    = models.CharField('Folder', max_length=250)


    file1 = models.FileField('file 1', upload_to=case_upload_location, null = True, blank = True)
    file2 = models.FileField('file 2', upload_to=case_upload_location, null = True, blank = True)
    file3 = models.FileField('file 3', upload_to=case_upload_location, null = True, blank = True)
    file4 = models.FileField('file 4', upload_to=case_upload_location, null = True, blank = True)

最终目标

Final Target

有多个文件要上传(用户需要一个一个地删除或更改,但要一次全部上传).

Multiple files to upload(user need to delete or change one by one but upload all at once).

推荐答案

看起来您需要从案例文件"模型到已定义的案例"模型的一对多外键关系.

Looks like you're in need one-many foreign key relationship from a "Case File" model to the "Case" model you've defined.

models.py

from django.db import models

def case_upload_location(instance, filename):
    case_name = instance.name.lower().replace(" ", "-")
    file_name = filename.lower().replace(" ", "-")
    return "casos/{}/{}".format(case_name, file_name)

class Case(models.Model):
    # datos del caso
    name = models.CharField('Nombre', max_length=250)
    observations = models.TextField('Observaciones', null = True, blank = True)
    number_folder = models.CharField('Numero de Carpeta', max_length=250)

class CaseFile(models.Model):
    case = models.ForeignKey(Case, on_delete=models.CASCADE) # When a Case is deleted, upload models are also deleted
    file = models.FileField(upload_to=case_upload_location, null = True, blank = True)

然后您可以添加StackedInline管理表单,以将案例文件直接添加到给定的案例中.

You can then add a StackedInline admin form to add Case Files directly to a given Case.

admin.py

from django.contrib import admin
from .models import Case, CaseFile

class CaseFileAdmin(admin.StackedInline):
    model = CaseFile

@admin.register(Case)
class CaseAdmin(admin.ModelAdmin):
    inlines = [CaseFileAdmin]

@admin.register(CaseFile)
class CaseFileAdmin(admin.ModelAdmin):
    pass

这篇关于如何从Django管理员上传多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆