title: Django REST Framework user registration Validation confusion
body: <p>I am trying to register a user with Django REST API with <code>two different methods</code></p>
  
  <p>With <code>method 1</code> everything works fine but it gives </p>
  
  <p>This problem :</p>
  
  <p><a href="https://stackoverflow.com/questions/43952178/django-rest-framework-user-registration-confusion">https://stackoverflow.com/questions/43952178/django-rest-framework-user-registration-confusion</a></p>
  
  <p>And with <code>method 2</code> it gives the following error <strong>but is creating users in backend</strong></p>
  
  <blockquote>
    <p>AttributeError at /api/users/register/ </p>
    
    <p>Got AttributeError when
    attempting to get a value for field <code>password2</code> on serializer
    <code>UserCreateSerializer</code>. The serializer field might be named
    incorrectly and not match any attribute or key on the <code>User</code> instance.
    Original exception text was: 'User' object has no attribute
    'password2'.</p>
  </blockquote>
  
  <p><strong><em>Code:</em></strong></p>
  
  <pre><code>from django.contrib.auth import get_user_model
  from rest_framework.exceptions import ValidationError
  
  from rest_framework.serializers import (ModelSerializer,CharField)
  
  User = get_user_model()
  
  
  class UserCreateSerializer(ModelSerializer):
      password2 = CharField(label='Confirm Password')
      class Meta:
          model = User
          fields = [
              'username',
              'password',
              'password2',
              'email',
          ]
          extra_kwargs = {
              'password' : {'write_only': True},
              'password2': {'write_only': True},
          }
  
      def validate_password(self, value):
          data = self.get_initial()
          password = data.get('password2')
          password2 = value
          if password != password2:
              raise ValidationError('Passwords must match')
          return value
  
      def validate_password2(self, value):
          data = self.get_initial()
          password = data.get('password')
          password2 = value
          if password != password2:
              raise ValidationError('Passwords must match')
          return value
  </code></pre>
  
  <p><strong>method 1:</strong></p>
  
  <pre><code>def create(self, validated_data):
      username = validated_data['username']
      email = validated_data['email']
      password = validated_data['password']
      user_obj = User(
          username=username,
          email=email
      )
      user_obj.set_password(password)
      user_obj.save()
      return validated_data
  </code></pre>
  
  <p><strong>method 2:</strong></p>
  
  <pre><code> def create(self, validated_data):
          user = User(
              email=validated_data['email'],
              username=validated_data['username'],
          )
          user.set_password(validated_data['password'])
          user.save()
          return user
  </code></pre>
  
  <blockquote>
    <p>Note: Using Python3 , Django 10.7 &amp; Django REST framework 3</p>
  </blockquote>