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

Django 的 FileField 如何上传到指定文件夹,但是只保存文件名,而不包含目录名?

  •  
  •   coolair · Feb 28, 2023 · 768 views
    This topic created in 1155 days ago, the information mentioned may be changed or developed.
    我知道有 upload_to 参数可以指定目录和文件名生成规则,比如:

    ```
    file = models.FileField('附件', upload_to='uploads/')
    ```

    这样写的话,图片是传到了 uploads 目录下,但是数据库里面 file 字段存的也是 uploads/library-bg1.jpg ,我想数据库只保存 library-bg1.jpg ,有没有办法实现?
    hackerfans
        1
    hackerfans  
       Feb 28, 2023
    视图里自己写方法,没必要非得用 FileField
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5615 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 06:53 · PVG 14:53 · LAX 23:53 · JFK 02:53
    ♥ Do have faith in what you're doing.