You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django项目图片上传异常:单个应用无法上传且路径未更新

Django图片上传异常排查求助

之前项目运行正常,编写ViewSet的update方法时突然出现图片无法上传的问题。更奇怪的是,项目里两个应用,一个能正常传图,另一个不行。修改了无法上传的字段的upload_to路径后,新路径没生效,数据库里还是旧路径,且图片从未成功上传。已经移除自定义update方法,问题仍存在,求排查。

更新接口返回结果(注意图片地址)

{
    "profile": 4,
    "card": "teste2",
    "points": 0,
    "created": "2022-11-29T16:05:33.502027Z",
    "updated": "2022-11-30T10:26:15.063888Z",
    "code": "673476",
    "image": "http://127.0.0.1:7000/medias/media/app_user/img_DENklQy.jpg"
}

模型代码(注意upload_to参数)

class MyCards(models.Model):
    profile = models.ForeignKey(AppUserProfile, on_delete=models.CASCADE)
    card = models.ForeignKey(Cards, on_delete=models.CASCADE)
    points = models.IntegerField(default=0)
    image = models.ImageField(upload_to='mycards', blank=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    code = models.CharField(max_length=6, blank=True, null=True)

配置信息

BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_URL = '/medias/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'medias' )

ViewSet代码(已移除自定义update方法)

class MyCardsViewSet(ModelViewSet):
    serializer_class = MyCardsSerializer
    queryset = MyCards.objects.all()

内容的提问来源于stack exchange,提问作者Victor Torres

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 12:45:34