Django rest framework user registration?

前端 未结 6 1497
傲寒
傲寒 2020-12-29 08:57

I am following this tutorial but facing these problems I can\'t fix:

  1. Upon registering user, I can not log in with that user to the api because the password is
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-29 09:08

    I tried the accepted answer in DRF 3.0.2 and it didn't work. The password was not being hashed.

    Instead, override the create method in your model serializer

        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
    

    This hashes the password when you create a user using the rest framework, not post_save

提交回复
热议问题