I am using rest framework serializer with graphql. I am trying to create a user when user signs up and create a profile based on the role user selects when creating an account. For this i have added a serializer layer, however the serializer create function is not called when i have used serializer.save
inside my mutate function. why is that so?
Here is how i have done
class Register(graphene.Mutation):
"""
Mutation to register a user
"""
class Arguments:
email = graphene.String(required=True)
password = graphene.String(required=True)
password_repeat = graphene.String(required=True)
role = graphene.String(required=True)
success = graphene.Boolean()
errors = graphene.List(graphene.String)
email = graphene.String()
def mutate(self, info, email, password, password_repeat, role):
if password == password_repeat:
try:
serializer = RegistrationSerializer(data={
'email': email,
'password': password,
'is_active': False,
'role': role,
})
if serializer.is_valid():
print("valid")
user = serializer.save()
# user.set_password(password)
print ("user is ###########", user)
# user = UserModel.objects.create(
# email=email,
# role=role,
# is_active=False
# )
# user.set_password(password)
# user.save()
print ("seraizlier after saving", serializer.validated_data.get('id'))
if djoser_settings.get('SEND_ACTIVATION_EMAIL'):
send_activation_email(user, info.context)
return Register(success=bool(user.id), email=user.email)
else:
print("serializer error", serializer.errors)
# TODO: specify exception
except Exception as e:
print("exception", e)
errors = ["email", "Email already registered."]
return Register(success=False, errors=errors)
errors = ["password", "Passwords don't match."]
return Register(success=False, errors=errors)
class RegistrationSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'email', 'role', 'is_active', 'password', )
def create(self, validated_data):
print ("validated_data", validated_data) # not logged in the terminal
user = User.objects.create(
email=validated_data['email'],
role=validated_data['role'],
is_active=validated_data['is_active']
)
user.set_password(validated_data['password'])
user.save()
print (" role ", validated_data['role'])
if (validated_data['role'] == "end_user" or validated_data['role'] == "agent"):
Profile.objects.create(user=user)
else:
Company.objects.create(user=user)
return user