diff --git a/program/serializers.py b/program/serializers.py index c157b9d30a71fd76c7e1ca8da5908ecee35484a7..e08726fc221f21416f7196da0c7b9668cf421f8e 100644 --- a/program/serializers.py +++ b/program/serializers.py @@ -32,7 +32,7 @@ from steering.settings import THUMBNAIL_SIZES class UserSerializer(serializers.ModelSerializer): # Add profile fields to JSON - profile = ProfileSerializer() + profile = ProfileSerializer(required=False) class Meta: model = User @@ -43,16 +43,18 @@ class UserSerializer(serializers.ModelSerializer): Create and return a new User instance, given the validated data. """ - profile_data = validated_data.pop('profile') + profile_data = validated_data.pop('profile') if 'profile' in validated_data else None user = super(UserSerializer, self).create(validated_data) user.date_joined = timezone.now() user.set_password(validated_data['password']) user.save() - profile = Profile(user=user, cba_username=profile_data.get('cba_username').strip(), - cba_user_token=profile_data.get('cba_user_token').strip()) - profile.save() + if profile_data: + profile = Profile(user=user, + cba_username=profile_data.get('cba_username').strip(), + cba_user_token=profile_data.get('cba_user_token').strip()) + profile.save() return user @@ -72,15 +74,18 @@ class UserSerializer(serializers.ModelSerializer): instance.is_staff = validated_data.get('is_staff', instance.is_staff) instance.is_superuser = validated_data.get('is_superuser', instance.is_superuser) - # TODO: How to hook into this from ProfileSerializer without having to call it here? - try: - profile = Profile.objects.get(user=instance.id) - except ObjectDoesNotExist: - profile = Profile.objects.create(user=instance, **validated_data['profile']) + profile_data = validated_data.pop('profile') if 'profile' in validated_data else None - profile.cba_username = validated_data['profile'].get('cba_username') - profile.cba_user_token = validated_data['profile'].get('cba_user_token') - profile.save() + if profile_data: + # TODO: How to hook into this from ProfileSerializer without having to call it here? + try: + profile = Profile.objects.get(user=instance.id) + except ObjectDoesNotExist: + profile = Profile.objects.create(user=instance, **profile_data) + + profile.cba_username = profile_data.get('cba_username') + profile.cba_user_token = profile_data.get('cba_user_token') + profile.save() instance.save() return instance