如何在django中指定文件上传到文件夹中以引用其中一个模型字段

时间:2018-01-15 09:42:54

标签: django django-models

我的文档应用程序中有以下django模型

class Section(models.Model):    
    choices = (
            ('Haematology', 'Haematology'),
            ('BloodBank', 'BloodBank'),
            ('Bacteriology', 'Bacteriology'),
            ('Parasitoloty', 'Parasitoloty'),
            ('Chemistry', 'Chemistry'),
            ('Histopathology', 'Histopathology'),
            ('Serology', 'Serology'),
           ('Immunology', 'Immunology'),
        )
    title = models.CharField(max_length = 50, choices = choices)

    class Meta:
        verbose_name = "Section"
        verbose_name_plural = "Sections"

    def __str__(self):
        return str(self.title)


class Document(models.Model, instance): 
    documentSection = models.ForeignKey(Section)    
    category = models.ForeignKey(DocumentCategory)
    title = models.CharField(max_length = 100, default = '')
    description = models.TextField(null = True, blank = True, default = '')
    documentFile = models.FileField(upload_to = 'uploads/' +  instance.documentSection.title)
    fileFormat = models.ForeignKey(FileFormat)
    uploaded = models.DateField(auto_now_add=True, default=timezone.now)
    modified = models.DateTimeField(auto_now=True, default=timezone.now)
    uploaded_by = models.ForeignKey(User)

    def __str__(self):
        return str(self.title)

当我上传我想要的文件然后保存在像'uploads / documentSection /这样的文件夹中 或'上传/ documentSection /%Y /%m /

我的问题是我无法弄清楚如何获取documentSection的值并将其解析为upload_to ='uploads / documentSection /

0 个答案:

没有答案