I have an ImageField. When I update it with the .update command, it does not properly save. It validates, returns a successful save, and says it is good. However, the image is never saved (I don't see it in my /media like I do my other pictures), and when it is later served, it is at /media/Raw%@0Data where there is no picture. When images are stored using a post it stores properly though. Any idea what is wrong, does it have to do with the nested serializer?
class MemberProfileSerializer(serializers.ModelSerializer):
class Meta:
model = MemberProfile
fields = (
'profile_image',
'phone_number',
'is_passenger',
'is_owner',
'is_captain',
'date_profile_created',
'date_profile_modified',
)
class AuthUserModelSerializer(serializers.ModelSerializer):
member_profile = MemberProfileSerializer(source='profile')
class Meta:
model = get_user_model()
fields = ('id',
'username',
'password',
'email',
'first_name',
'last_name',
'is_staff',
'is_active',
'date_joined',
'member_profile',
)
def update(self, instance, validated_data):
profile_data = validated_data.pop('profile')
for attr, value in validated_data.items():
if attr == 'password':
instance.set_password(value)
else:
setattr(instance, attr, value)
instance.save()
if not hasattr(instance, 'profile'):
MemberProfile.objects.create(user=instance, **profile_data)
else:
#This is the code that is having issues
profile = MemberProfile.objects.filter(user=instance)
profile.update(**profile_data)
return instance
Above, you see where profile = MemberProfile.objects.filter(user=instance) and then the update command. That is not properly saving the image in accordance with the model.
class MemberProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, unique=True, related_name='profile')
profile_image = models.ImageField(
upload_to=get_upload_path(instance="instance",
filename="filename",
path='images/profile/'),
blank=True)