register login serializer django

Solutions on MaxInterview for register login serializer django by the best coders in the world

showing results for - "register login serializer django"
Jonathon
29 Oct 2016
1from rest_framework import serializers
2from rest_framework.validators import UniqueValidator
3from django.contrib.auth.models import User
4
5class UserSerializer(serializers.ModelSerializer):
6    email = serializers.EmailField(
7            required=True,
8            validators=[UniqueValidator(queryset=User.objects.all())]
9            )
10    username = serializers.CharField(
11            validators=[UniqueValidator(queryset=User.objects.all())]
12            )
13    password = serializers.CharField(min_length=8)
14
15    def create(self, validated_data):
16        user = User.objects.create_user(validated_data['username'], validated_data['email'],
17             validated_data['password'])
18        return user
19
20    class Meta:
21        model = User
22        fields = ('id', 'username', 'email', 'password')
23