django FileFIeld和ImageField 上传路径改写

def get_file_path(instance, filename):
    return file/document/%s/%s/%s % (instance.period.code, instance.student.college.code, filename)


class Document(models.Model):
    title = models.CharField(max_length=50, blank=True, verbose_name="文档名", default="")
    student = models.ForeignKey(accounts.Student, verbose_name="学生")
    period = models.ForeignKey(systemconfig.Periods, verbose_name="阶段")
    file = models.FileField(upload_to=get_file_path, verbose_name="文档", blank=True, null=True)
    mark = models.SmallIntegerField(null=True, blank=True, verbose_name="标记")
    create_time = models.DateTimeField(verbose_name="添加时间", auto_now_add=True)

    class Meta:
        verbose_name = "学生报告文档"
        verbose_name_plural = "学生报告文档"

重点就是get_file_path

def get_file_path(instance, filename):
return ‘file/document/%s/%s/%s‘ % (instance.period.code, instance.student.college.code, filename)


class Document(models.Model):
title = models.CharField(max_length=50, blank=True, verbose_name="文档名", default="")
student = models.ForeignKey(‘accounts.Student‘, verbose_name="学生")
period = models.ForeignKey(‘systemconfig.Periods‘, verbose_name="阶段")
file = models.FileField(upload_to=get_file_path, verbose_name="文档", blank=True, null=True)
mark = models.SmallIntegerField(null=True, blank=True, verbose_name="标记")
create_time = models.DateTimeField(verbose_name="添加时间", auto_now_add=True)

class Meta:
verbose_name = "学生报告文档"
verbose_name_plural = "学生报告文档"

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。