I'm starting my first project with django rest framework.
I've create my own custom user by changing the login with email.
I want first to register an user but I'm already stuck. I've got the error 'confirm_password' is an invalid keyword argument for this function when I try to create one.
Here is my serialiser:
class AccountSerializer(serializers.ModelSerializer):
password = serializers.CharField(write_only=True, required=False)
confirm_password = serializers.CharField(write_only=True, required=False)
class Meta:
model = Account
fields =('id', 'email', 'username', 'date_joined', 'last_login', 'first_name', 'last_name', 'password', 'confirm_password')
read_only_fields = ('last_login', 'date_joined')
def create(self, validated_data):
return Account.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.first_name = validated_data.get('first_name', instance.first_name)
instance.last_name = validated_data.get('last_name', instance.last_name)
instance.save()
password = validated_data.get('password', None)
confirm_password = validated_data.get('confirm_password', None)
if password and confirm_password and password == confirm_password:
instance.set_password(password)
instance.save()
update_session_auth_hash(self.context.get('request'), instance)
return instance
Here is my view:
class AccountViewSet(viewsets.ModelViewSet):
# lookup_field = 'username'
queryset = Account.objects.all()
serializer_class = AccountSerializer
def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.AllowAny(),)
if self.request.method == 'POST':
return (permissions.AllowAny(),)
return (permissions.IsAuthenticated(), IsAccountOwner(),)
def create(self, request):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
Account.objects.create_user(**serializer.validated_data)
return Response(serializer.validated_data, status=status.HTTP_201_CREATED)
return Response({
'status': 'Bad request',
'message': 'les données ne sont pas valides'
}, status=status.HTTP_400_BAD_REQUEST)
And here is my custom user I extends from the django-custom-user :
class Account(AbstractEmailUser):
username = models.CharField(unique=True, max_length=50)
first_name = models.CharField(blank=True, null=True, max_length=50)
last_name = models.CharField(blank=True, null=True, max_length=50)
I understand I've got the error because I've not a field confirm password in my model but how do I verify the password without a double input?
I can't find any documentation or tutorial about register/login with the django rest framework so if you know some I'm really interesting in !
A bit more reading and seems like ModelSerializer
doesn't support non-model field for writable, so one way to do it may be using a normal Serializer
class, like so:
class AccountSerializer(serializers.Serializer):
email = serializers.EmailField(required=True)
username = serializers.CharField(required=True)
password = serializers.CharField(required=True)
confirmed_password = serializers.CharField(write_only=True)
# ... and your other fields ...
def validate(self, data):
if data['password'] != data['confirmed_password']:
raise serializers.ValidationError("passwords not match")
return data
def create(self, validated_data):
# ... your create stuff ...
def update(self, instance, validated_data):
# ... and your update stuff ...
And you can then use the serializer like a form in your viewset:
In [17]: data = {'confirmed_password': '1234',
'email': 'hello@world.com',
'password': '1234',
'username': 'test'}
In [18]: serializer = AccountSerializer(data=data)
In [19]: serializer.is_valid()
Out[19]: True
In [20]: serializer.data
Out[21]: {'email': u'hello@world.com', 'username': u'test', 'password': u'1234'}
In [23]: data = {'confirmed_password': '123456789',
'email': 'hello@world.com',
'password': '1234',
'username': 'test'}
In [24]: serializer = AccountSerializer(data=data)
In [25]: serializer.is_valid()
Out[26]: False
In [27]: serializer.errors
Out[27]: {u'non_field_errors': [u'passwords not match']}
Here is the doc where you can read more about the serializers
, hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.