django - 当我在 django 中更新我的个人资料模型中的其他字段时,如何保留我的 `profile_pic`?
问题描述
我正在尝试更新我的UserProfile
模型,但问题是,每当我更新其他字段时,profile_pic
它都设置为 null。profile_pic
更新其他字段时如何保留?
这是我的models.py
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.conf import settings
class CustomUser(AbstractUser):
username = models.CharField(max_length=100, blank=True, null=True)
email = models.EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'first_name', 'last_name']
def __str__(self):
return self.email
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='profile')
title = models.CharField(max_length=5, blank=True, null=True)
dob = models.DateField(auto_now=False, auto_now_add=False, null=True)
address = models.CharField(max_length=255, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
zip = models.CharField(max_length=10, blank=True, null=True)
country = models.CharField(max_length=50, blank=True, null=True)
profile_pic = models.ImageField(default='user_default_m.png', upload_to='profile/', blank=True, null=True)
def __str__(self):
return f'{self.user.username} Profile'
这是我的serializer.py
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('title', 'dob', 'address', 'country', 'city', 'zip', 'profile_pic',)
class CustomUserDetailsSerializer(serializers.HyperlinkedModelSerializer):
profile = UserProfileSerializer(required=True)
class Meta:
model = CustomUser
fields = ('email', 'first_name', 'last_name', 'profile')
# extra_kwargs = {'password': {'write_only': True}}
def create(self, validated_data):
profile_data = validated_data.pop('profile')
password = validated_data.pop('password')
user = CustomUser(**validated_data)
user.set_password(password)
user.save()
UserProfile.objects.create(user=user, **profile_data)
return user
def update(self, instance, validated_data):
profile_data = validated_data.pop('profile')
profile = instance.profile
instance.email = validated_data.get('email', instance.email)
instance.save()
profile.title = profile_data.get('title', profile.title)
profile.dob = profile_data.get('dob', profile.dob)
profile.address = profile_data.get('address', profile.address)
profile.country = profile_data.get('country', profile.country)
profile.city = profile_data.get('city', profile.city)
profile.zip = profile_data.get('zip', profile.zip)
profile.profile_pic = profile_data.get('profile_pic', profile.profile_pic)
profile.save()
return instance
我如何确保它profile_pic
是持久的?
解决方案
在你的models.py
,删除 null=True,所以改变
profile_pic = models.ImageField(default='user_default_m.png', upload_to='profile/', blank=True, null=True)
到
profile_pic = models.ImageField(default='user_default_m.png', upload_to='profile/', blank=True)
推荐阅读
- reactjs - 如何通过 React + Next.js 在 Material UI 中使用 Theme?
- sql - 获取记录对表的位置(ORDER BY ID)
- angular - 默认滑块为所有其他滑块设置相同的值
- architecture - 事务管理器 2 阶段提交方法和 saga SEC 方法有什么区别?
- mysql - mysql 满足条件就抓一行,不满足就抓另一个
- python - matplotlib.pyplot.plot() 如何连接散点?
- php - 带有简单 html dom 解析器的垃圾 Javascript 和 Css 代码
- python - 在函数 Tkinter python gui 中定义的标签内添加图像时出错
- ruby-on-rails - Rails 4数据表导出到excel文件正在工作,但文件不可读
- python - 无法导入 cvxpy & 无法导入名称和求解链