title: Django : profile edition set_password()
body: <p>I would like to create an API for a mobile application. In this application, we can create an account, and of course, edit our profile.
  For create an account, I use the django account default model like that:
  <em>models.py</em>:</p>
  <pre><code>    def create_user(self, email, username, phone, deliveryAddress, postalCode, city, password=None):
          if not email:
              raise ValueError(&quot;Users must have an email address&quot;)
          if not username:
              raise ValueError('Users must have an username')
          if not phone:
              raise ValueError('Users must have a phone number')
          if not deliveryAddress:
              raise ValueError('Users must have a delivery Address')
          if not postalCode:
              raise ValueError(&quot;Users must have a postal code&quot;)
          if not city:
              raise ValueError('Users must have a city')
  
          user = self.model(
              email = self.normalize_email(email),
              username = username,
              phone = phone,
              deliveryAddress = deliveryAddress,
              postalCode = postalCode,
              city = city,
          )
  
          user.set_password(password)
          user.save(using = self._db)
          return user
  
      def create_superuser(self, email, username, phone, deliveryAddress, postalCode, city, password=None):
  
          user = self.create_user(
              email = self.normalize_email(email),
              username = username,
              password = password,
              phone = phone,
              deliveryAddress = deliveryAddress,
              postalCode = postalCode,
              city = city,
          )
  
          user.is_admin = True
          user.is_staff = True
          user.is_superuser = True
          user.save(using = self._db)
  
  class memberArea(AbstractBaseUser):
      username = models.CharField(max_length=255)
      email = models.EmailField(max_length=255, unique=True)
      phone = models.TextField()
      date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
      last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
      deliveryAddress = models.TextField()
      postalCode = models.CharField(max_length=255)
      forget = models.TextField(null=True, blank=True)
      city = models.CharField(max_length=255)
      is_admin = models.BooleanField(default=False)
      is_active = models.BooleanField(default=True)
      is_staff = models.BooleanField(default=False)
      is_superuser = models.BooleanField(default=False)
  
      USERNAME_FIELD = 'email'
      REQUIRED_FIELDS = ['username', 'phone', 'deliveryAddress', 'postalCode', 'city']
  
      objects = MyAccountManager()
  
      def __str__(self):
          return self.email + ' : ' + self.username
  
      def has_perm(self, perm, obj=None):
          return self.is_admin
  
      def has_module_perms(self, app_label):
          return True
  
  @receiver(post_save, sender=settings.AUTH_USER_MODEL)
  def create_auth_token(sender, instance=None, created=False, **kwargs):
      if created:
          Token.objects.create(user=instance)
  </code></pre>
  <p>When I create an account, I automaticaly encrypt the user password with <strong>set_password()</strong>
  Now, I want to make a view to edit an account.
  Of course I want the password to be encrypted too.
  This is my code:
  <em>serializer.py</em>:</p>
  <pre><code>class AccountSerializer(serializers.ModelSerializer):
      class Meta:
          model = memberArea
          fields = ['username', 'email', 'phone', 'password', 'deliveryAddress', 'postalCode', 'city']
          extra_kwargs = {
              'password': {'write_only': True},
          }
  </code></pre>
  <p><em>views.py</em>:</p>
  <pre><code>#Edit account
  @api_view(['PUT', ])
  def edit_account(request, pk):
      try:
          account = memberArea.objects.get(pk=pk)
      except memberArea.DoesNotExist:
          return HttpResponse(status=404)
  
      if request.method == 'PUT':
          serializer = AccountSerializer(account, data=request.data)
          data = {}
          if serializer.is_valid():
              serializer.save()
              data['response'] = 'Account updated with success !'
              return Response(data)
          return Response(serializer.errors)
  </code></pre>
  <p>I don't know where and how I can encrypt the password before updating the profile.
  Maybe I can do that in my serializer file by finding the account that we want to edit but I don't know how to do that in this file...
  Thanks by advance for your help</p>