1

I am trying to create users using post request within django rest framework.

Here is my serializer code:

class UserSerializer(serializers.ModelSerializer):
    favorites = serializers.PrimaryKeyRelatedField(many=True, queryset=Favorite.objects.all())
    class Meta:
        model = User
        fields = ('id', 'username','password','favorites')
        extra_kwargs = {
            'password': {
                'write_only': True,
            },
            'confirm_password': {
                'write_only': True,
            },
            'favorites':
            {
                'read_only': True,
            },
        }

    def create(self, validated_data):
        return User.objects.create(**validated_data) 

for create method i used also this implementation:

def create(self, validated_data):
    user = User(username=validated_data['username'])
    user.set_password(validated_data['password'])
    user.save()
    return user

In my admin panel it says for the created user:

Invalid password format or unknown hashing algorithm.

What should i change ?

lukeaus
  • 11,465
  • 7
  • 50
  • 60
Mejdi Lassidi
  • 999
  • 10
  • 22

2 Answers2

4

There's a special method on the User's manager - create_user(), which do exactly what you need to do

yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42
1

This answer fixed it: https://stackoverflow.com/a/29391122/355641

i guess the use of get_user_model was the solution.

Community
  • 1
  • 1
Mejdi Lassidi
  • 999
  • 10
  • 22