Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new user with django rest framework and custom user model

I have a custom user model and I am using django-rest-framework to create API

models.py:

class User(AbstractBaseUser, PermissionsMixin):     email = models.EmailField(         unique=True,         max_length=254,     )     first_name = models.CharField(max_length=15)     last_name = models.CharField(max_length=15)     mobile = models.IntegerField(unique=True)     date_joined = models.DateTimeField(default=timezone.now)     is_active = models.BooleanField(default=True)     is_admin = models.BooleanField(default=False) 

serializers.py:

class UserSerializer(serializers.ModelSerializer):     password1 = serializers.CharField(write_only=True)     password2 = serializers.CharField(write_only=True)      class Meta:         model = User         fields = ('first_name', 'last_name', 'email', 'mobile', 'password1', 'password2') 

views.py:

@api_view(['POST']) @permission_classes((AllowAny,)) def create_user(request):     serialized = UserSerializer(data=request.data)     if serialized.is_valid():         User.objects.create_user(             serialized.save()         )         return Response(serialized.data, status=status.HTTP_201_CREATED)     else:         return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST) 

However, when I try to create a new user I am getting this error:

Got a TypeError when calling User.objects.create(). This may be because you have a writable field on the serializer class that is not a valid argument to User.objects.create(). You may need to make the field read-only, or override the UserSerializer.create() method to handle this correctly.

This maybe because there's no password1 or password2 fields in the User model. But so, how can I create an API to create a new user using django-rest-framework?

like image 878
Aamu Avatar asked Mar 26 '17 16:03

Aamu


People also ask

Should you create a custom user model for new projects in Django?

It's highly recommended to set up a custom User model when starting a new Django project. Without it, you will need to create another model (like UserProfile ) and link it to the Django User model with a OneToOneField if you want to add new fields to the User model.


1 Answers

I think one password field is enough. If you want to check the user's twice password input is same, do it in the front-end. You can override a create method from serializer like following.

from rest_framework import serializers  class UserSerializer(serializers.ModelSerializer):     password = serializers.CharField(write_only=True)      class Meta:         model = User         fields = ('first_name', 'last_name', 'email', 'mobile', 'password')      def create(self, validated_data):         user = super(UserSerializer, self).create(validated_data)         user.set_password(validated_data['password'])         user.save()         return user 

views.py

from rest_framework import generics from rest_framework.permissions import AllowAny from .models import User from .serializers import UserSerializer  class UserCreateAPIView(generics.CreateAPIView):     queryset = User.objects.all()     serializer_class = UserSerializer     permission_classes = (AllowAny,) 
like image 65
Xiao Avatar answered Sep 17 '22 11:09

Xiao