SMART-29 created serializers for user model

This commit is contained in:
s460930 2020-11-30 22:54:03 +01:00
parent 60fac95880
commit df48b97ea8

View File

@ -0,0 +1,40 @@
"""
@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