首页 > 解决方案 > 当我在 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是持久的?

标签: djangodjango-modelsdjango-rest-frameworkdjango-serializer

解决方案


在你的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)

推荐阅读