Last active
December 29, 2015 12:39
-
-
Save meoooh/7672322 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./manage.py shell | |
Python 2.7.3 (default, Sep 26 2013, 20:03:06) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
(InteractiveConsole) | |
>>> from app.serializers import * | |
>>> u=UserSerializer(data={'username':'test1','password':'pass','password2':'pass'}) | |
>>> u.errors | |
Traceback (most recent call last): | |
File "<console>", line 1, in <module> | |
File "/home/han/.virtualenvs/drf_test/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 507, in errors | |
ret = self.from_native(data, files) | |
File "/home/han/.virtualenvs/drf_test/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 919, in from_native | |
instance = super(ModelSerializer, self).from_native(data, files) | |
File "/home/han/.virtualenvs/drf_test/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 348, in from_native | |
return self.restore_object(attrs, instance=getattr(self, 'object', None)) | |
File "/home/han/.virtualenvs/drf_test/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 903, in restore_object | |
instance = self.opts.model(**attrs) | |
File "/home/han/.virtualenvs/drf_test/local/lib/python2.7/site-packages/django/db/models/base.py", line 417, in __init__ | |
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0]) | |
TypeError: 'password2' is an invalid keyword argument for this function | |
>>> u=UserSerializer(data={'username':'test1','password':'pass','password2':'pas'}) | |
>>> u.errors | |
{'password2': [u"The two password fields didn't match."]} | |
>>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth.models import User | |
from rest_framework import serializers | |
class UserSerializer(serializers.ModelSerializer): | |
password2 = serializers.CharField() | |
class Meta: | |
model = User | |
fields = ('username', 'password', 'password2') | |
def validate_password2(self, attrs, source): | |
if attrs.get('password') == attrs.get('password2'): | |
return attrs | |
raise serializers.ValidationError( | |
_("The two password fields didn't match."), | |
code='password_mismatch', | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment