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 = "学生报告文档"

posted on 2015-04-01 17:23  颓废的悠然  阅读(2148)  评论(0编辑  收藏  举报

导航