SmartPicasso/rest-app/smartpicasso/app/user/serializers.py

41 lines
1.1 KiB
Python

"""
@author p.dolata
"""
from rest_framework import serializers
from smartpicasso.app.user.models import User
from smartpicasso.app.user_profile.models import UserProfile
class UserSerializer(serializers.ModelSerializer):
"""
Class to manage serializing UserProfile
"""
class Meta:
model = UserProfile
fields = ('first_name', 'last_name', 'phone_number')
class UserRegistrationSerializer(serializers.ModelSerializer):
"""
Class to manage serializing user during registration
"""
profile = UserSerializer(required=False)
class Meta:
model = User
fields = ('email', 'password', 'profile')
extra_kwargs = {'password': {'write_only': True}}
def create(self, validated_data):
profile_data = validated_data.pop('profile')
user = User.objects.create_user(**validated_data)
UserProfile.objects.create(
user=user,
first_name=profile_data['first_name'],
last_name=profile_data['last_name'],
phone_number=profile_data['phone_number'],
)
return user