coolair
V2EX  ›  问与答

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

  •  
  •   coolair · Feb 28, 2023 · 819 views
    This topic created in 1194 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   ·   3022 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 09:02 · PVG 17:02 · LAX 02:02 · JFK 05:02
    ♥ Do have faith in what you're doing.