Reputation: 999
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 ?
Upvotes: 1
Views: 815
Reputation: 999
This answer fixed it: https://stackoverflow.com/a/29391122/355641
i guess the use of get_user_model
was the solution.
Upvotes: 1
Reputation: 9359
There's a special method on the User
's manager - create_user(), which do exactly what you need to do
Upvotes: 4