How to save FileField with None in Django?

坚强是说给别人听的谎言 提交于 2021-01-29 07:16:26

问题


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)

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)

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


回答1:


change you model

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

remove the blank option



来源:https://stackoverflow.com/questions/53312736/how-to-save-filefield-with-none-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!