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)
As noted in the docs,
.update()
doesn't call the model.save()
or fire thepost_save
/pre_save
signals for each matched model. It almost directly translates into a SQLUPDATE
statement. https://docs.djangoproject.com/en/1.8/ref/models/querysets/#updateWhile it isn't obvious from the docs, the uploaded file is saved to disk as part of the model
.save()
as well: https://docs.djangoproject.com/en/1.8/topics/files/#using-files-in-modelsThis means you can use
.update()
to directly change the path value stored in the DB column, but it assumes that the file has already been saved to disk in that location.The easiest way to work around this issue is to call
.save()
in both both paths..create()
already calls.save()
so you would need to change the.update()
version to something such as: