如何在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)
删除空白
选项