如何在Django中将FileField保存为None? [英] How to save FileField with None in Django?

查看:69
本文介绍了如何在Django中将FileField保存为None?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有Avatar字段的模型Profile,使用FileField.

I have model Profile with Avatar field use FileField.

class Profile(models.Model):
    avatar = models.FileField("Uploaded avatar of profile", storage=OverwriteStorage(), upload_to=avatar_photo_upload, null=True, blank=True)

我编写了删除头像的函数,该函数将头像= None

I write function to remove avatar, which set avatar = None

def remove_avatar(self, request):
        profile = Profile.objects.get(user=request.user)
        profile.avatar = None
        profile.avatar.save()
        return Response({ 'status': 'ok', 'message': 'Avatar successfully removed' }, status=status.HTTP_200_OK)

在我的数据库中,化身存储为'',而不是 null .为什么以及如何解决?

In my database, avatar stored as '', not null. Why and how can I fix it?

推荐答案

更改模型

class Profile(models.Model):
    avatar = models.FileField("Uploaded avatar of profile", storage=OverwriteStorage(), upload_to=avatar_photo_upload, null=True)

删除空白选项

这篇关于如何在Django中将FileField保存为None?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆