[Vuejs]-How to make Vue.js post hashed password to Django REST API AbstractBaseUser custom model?

0👍

@dethos He gave me a clue for this.

So finally I found a solution here

Why isn't my Django User Model's Password Hashed?

And I will share my edited serializers.py

from rest_framework import serializers
from .models import User


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        # Model set
        model = User
        # Field set
        fields = ('id', 'username', 'email', 'organization', 'full_name', 'phone', 'password',
                  'is_superuser', 'is_active', 'is_staff', 'last_login', 'created_date', 'updated_date')

    def create(self, validated_data):
        password = validated_data.pop('password', None)
        is_active = validated_data.pop('is_active', True)
        instance = self.Meta.model(**validated_data)
        if password is not None:
            instance.set_password(password)
        instance.save()
        return instance

    def update(self, instance, validated_data):
        for attr, value in validated_data.items():
            if attr == 'password':
                instance.set_password(value)
            else:
                setattr(instance, attr, value)
        instance.save()
        return instance

I added ‘set_password’, and ‘is_active”s default value.

Leave a comment