django 对models中上传的文件或图片改名
def user_directory_path(instance,filename): print instance,filename ext = filename.split('.')[-1] print 'ext',ext filename = '{}.{}'.format(uuid.uuid4().hex[:10], ext) print filename # print os.path.join(instance.user.id, "avatar", filename) return os.path.join("uepload", filename)
class Course(models.Model): detail = UEditorField(verbose_name="课程详情",width=600, height=300, imagePath="courses/",filePath="courses/", default='') is_banner = models.BooleanField(default=False, verbose_name="是否轮播") degree = models.CharField(verbose_name="难度", choices=(("cj","初级"), ("zj","中级"), ("gj","高级")), max_length=2) fav_nums = models.IntegerField(default=0, verbose_name='收藏人数') image = models.ImageField(upload_to=user_directory_path, verbose_name="封面图", max_length=100) click_nums = models.IntegerField(default=0, verbose_name="点击数")
class Meta:
verbose_name = "课程"
erbose_name_plural = verbose_name
定义一个函数,在函数中使用uuid.uuid4().hex[:10],生成一个10位随机数,返回给model类