Michael Ajanaku
Michael Ajanaku

Reputation: 95

AssertionError: `create()` did not return an object instance

I am getting the error below while sending a request to a UserRegisterView:

  File "/Users/MichaelAjanaku/Desktop/test/afrocode/lib/python3.6/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/MichaelAjanaku/Desktop/test/leave/views.py", line 22, in post
    serializer.save()
  File "/Users/MichaelAjanaku/Desktop/test/afrocode/lib/python3.6/site-packages/rest_framework/serializers.py", line 207, in save
    '`create()` did not return an object instance.'
AssertionError: `create()` did not return an object instance.

I don't know why such occurs. Here is my views.py:

class UserRegistrationView(CreateAPIView):
    serializer_class = UserRegistrationSerializer
    permission_classes = (AllowAny,)

    def post(self, request):
        serializer = self.serializer_class(data= request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        status_code = status.HTTP_201_CREATED
        response = {
            'success' : 'True',
            'status code' : status_code,
            'message' : 'User registered successfully'
        }

and the serializers:

class UserRegistrationSerializer(serializers.ModelSerializer):

    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'],
                     )

Please what is causing the error? Thanks

Upvotes: 3

Views: 5259

Answers (3)

Random User
Random User

Reputation: 609

I had the same error too. In your serializer.py : at the top add:

from django.contrib.auth import get_user_model
User = get_user_model()

This worked for me as I was using a custom user.

Upvotes: 0

jordy bayo
jordy bayo

Reputation: 449

you did not return the user in your create method.

class UserManager(BaseUserManager):

def create_user(self, username, email, password=None):
    if username is None:
        raise TypeError('User should have a username')
    if email is None:
        raise TypeError('User should have an email')

    user = self.model(username=username, email=self.normalize_email(email))
    user.set_password(password)
    user.save()
    return user

Upvotes: 0

Joseph
Joseph

Reputation: 13178

Your create method is not returning an object instance. You should return the result of the User.objects.create(...) call.

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'],
                 )
    return user

Upvotes: 7

Related Questions