V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
dolaxi
V2EX  ›  问与答

Django 图片上传,如何用自增 id 作为图片的名

  •  
  •   dolaxi · Jan 5, 2020 · 1702 views
    This topic created in 2310 days ago, the information mentioned may be changed or developed.
    我本意是想图片上传的时候,用数据库自增 id 作为图片的名称

    models.py

    '''
    class Goods(models.Model):
    image = models.ImageField(upload_to=pic_path,verbose_name='封面图')
    create_time = models.DateTimeField(auto_now_add=True,verbose_name='创建时间')


    def pic_path(instance, filename):
    #自定义保存路径和文件名

    # 源文件名后缀
    ext = filename.split('.')[-1]
    # 文件名
    # new_name = datetime.now().strftime('%Y%m/')+shortuuid.uuid()
    # new_name =instance.id #无效
    new_name =

    # 拼接文件名和后缀
    path = new_name + '.' + ext
    return path

    '''
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2363 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 10:32 · PVG 18:32 · LAX 03:32 · JFK 06:32
    ♥ Do have faith in what you're doing.