serializer create function is not called
Asked Answered
A

1

5

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
Ancon answered 21/3, 2019 at 16:5 Comment(0)
W
12

You have an indentation typo. create should be a method of RegistrationSerializer, but as written, it's a method of the Meta class.

Fix the indentation and you should be good to go:

class RegistrationSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'email', 'role', 'is_active', 'password', )

    def create(self, validated_data):
        ...
Walker answered 21/3, 2019 at 16:17 Comment(2)
did not notice that. Thanks a lot for your help.Ancon
I made exactly the same error. What are the odds? :-D This really helped!Howbeit

© 2022 - 2024 — McMap. All rights reserved.