【问题标题】:Uploading a file in django database in a specific root with diferrent folders everytime每次在具有不同文件夹的特定路径中上传 django 数据库中的文件
【发布时间】:2018-11-02 16:40:00
【问题描述】:

我正在尝试将文件上传到数据库中,但是当我想将函数 uoload 放入并且我希望该函数将文件与用户提交的数据一起存储在根目录中时,例如年份,课程,部分,我得到了他们上传的信息和文件我想存储在那个子文件夹中,我正在使用的代码只将数据存储在 Media_root 中,而不是在地毯中 我该怎么做才能将文件存储在我想要的子文件夹。我是我的模型文档,我存储文档的信息,如作者、标题等。

class File(models.Model):
     titulo_file=models.ForeignKey(Document,on_delete=models.CASCADE,null=True,verbose_       name='Título de la Tesis')
     file = models.FileField(null=True,blank=True, upload_to=generate_path)

这是我用来上传文件的函数,Int 只用于将文件存储到同名的文件夹中。

def generate_path(instance, filename):

      path =os.path.join(instance.file.name,filename)
      return path

在我看来,在用户提交信息后,我使用os.path.join,即用提交的信息做路径:

year/course/section

我想将该路径发送到我的 generate_path 并将文件存储在该位置,我尝试使用 session 但它不起作用,我该怎么办?

models.py

class Year(models.Model):
    year_number=models.CharField(max_length=10,primary_key=True)

class Course(models.Model):
    name=models.CharField(max_length=50,primary_key=True)

class Section(models.Model):
    numberSection=models.IntegerField(null=True,blank=True)
    year = models.ForeignKey(Year, on_delete=models.CASCADE, null=True)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)

thopic_documents=(
       ('CS','CS'),
       ('SE','SE'),
       ('IS','IS'),
       ('IT','IT'),
    )

class Document(models.Model):
     title=models.CharField(max_length=200,primary_key=True)
     thopic=models.CharField(max_length=50,choices=thopic_documents, default=None,null=True)

class Historial(models.Model):
      id_section=models.ForeignKey(Section,on_delete=models.CASCADE)
      title_document=models.ForeignKey(Document,on_delete=models.CASCADE,)

【问题讨论】:

  • 您能发布包含yearcoursesection 属性的模型吗?那是Document 模型,还是其他模型?
  • 是其他型号
  • 谢谢。因此,如果您有Document,您将如何查找Section?那可能吗?可以通过Historical 或其他模型以某种方式完成吗?
  • 我浏览了带有历史的部分,部分有模型课程和年份的信息。

标签: python django file-upload


【解决方案1】:

要获得年份/课程/部分,您需要能够从 File 导航到 Section。但是,DocumentSectionHistorical 之间存在的多对多关系使您不清楚您将如何做到这一点。你会选择哪个Historical

如果File 直接链接到Historical 可能会更好:

class File(models.Model):
     titulo_file=models.ForeignKey(Historical, on_delete=models.CASCADE,null=True,verbose_name='Título de la Tesis')
     file = models.FileField(null=True,blank=True, upload_to=generate_path)

如果是这种情况,您可以实现您的generate_path(),例如:

def generate_path(instance, filename):
    section = instance.titulo_file.id_section
    year = section.year.year_number
    course = section.course.name
    return os.path.join(str(year), course, str(section.numberSection), filename)

要对当前模型执行相同的操作,您必须执行以下操作:

def generate_path(instance, filename):
    section = instance.titulo_file.historical_set.first().id_section
    year = section.year.year_number
    course = section.course.name
    return os.path.join(str(year), course, str(section.numberSection), filename)

该示例使用historical_set.first() 来获取第一个Historical 链接Document。也许这样可以,但否则您需要知道要使用哪个Historical

如果上传了同名文件,又不想覆盖,可以实现自己的storage

from django.core.files.storage import FileSystemStorage

class UseExistingStorage(FileSystemStorage):

    def save(self, name, content, max_length=None):
        if not self.exists(name):
            return super().save(name, content, max_length)
        return name  # Don't save when the file exists, just return the name

然后从您的File 模型中引用UseExistingStorage

file = models.FileField(null=True,blank=True, upload_to=generate_path, storage=UseExistingStorage())

【讨论】:

  • 但我需要有模型文档并附加到它我需要有文件模型。在 Document 中,标题是 pk,historiacal 有模型 Seccion 的标题和 id。我需要让我的模型保持这样,我现在的模型没有办法做我想做的事吗?
  • 每个Document 可以有许多Historicals,因此对于您当前的模型,您需要知道要使用哪个Historical 才能获得Section。是最新的Historical 还是特定的?如果您知道,那么您可以使用当前的模型
  • 我已经更新了答案,并举例说明了如何使用当前模型执行此操作,以及识别使用哪个 Historical 时遇到的问题。
  • 抱歉刚刚回答,但我收到此错误object of type 'NoneType' has no len()
  • 如果我想得到最后一个历史,而不是第一个,我把 lastest()?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多