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 = "学生报告文档"
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:django FileFIeld和ImageField 上传路径改写 - Python技术站