I am trying to update my UserProfile
model, however the issue is, whenever I update other fields the profile_pic
is set to null. How do I retain the profile_pic
when I update other fields?
This is my 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'
This is my 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
How do I ensure that profile_pic
is persistant?
CodePudding user response:
In your models.py
, remove null=True, so change
profile_pic = models.ImageField(default='user_default_m.png', upload_to='profile/', blank=True, null=True)
to
profile_pic = models.ImageField(default='user_default_m.png', upload_to='profile/', blank=True)