[Django]-AssertionError: `create()` did not return an object instance

6👍

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
👤Joseph

0👍

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

0👍

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.

Leave a comment