Suppose I want to register a user (I'm using the User model located in django.contrib.auth.models). Assume this is my serializers.py:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'password', 'email', )
What's the difference between the following views, and which one is recommended when it comes to creating a user?
View A:
def createUser(request):
if request.method == 'POST':
serializer = UserSerializer(data=request.DATA)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
View B:
def createUser(request):
serializer = UserSerializer(data=request.DATA)
if serializer.is_valid():
user = User.objects.create_user(
email = serializer.init_data['email'],
username = serializer.init_data['username'],
password = serializer.init_data['password'],
)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Neither of those is perfect. But View A looks promising.
User
creation is not simply User.save
, but rather you have to call User.create_user
method.User.create_user
, however, the views contains a logic which should actually be abstracted within the Serializer.save()
method.To solve this, you have to change the behavior of Serializer.save()
method. Looking at the documentation, Serializer.save()
, will call either Serializer.create()
or Serializer.update()
.
In summary, we have to override the Serializer.create()
for user creation and use View A.
# File: serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
def create(self, validated_data):
user = User.objects.create_user(
email = validated_data['email'],
username = validated_data['username'],
password = validated_data['password'],
)
return user