简体   繁体   中英

Python argument of type 'NoneType' is not iterable

I'm getting an error when posting the following json: {"email":"test@test.com", "password":"12345", "repeatPassword":"12345"}

I'm using Django-Rest_framework, so I think I might have set up something wrong?

This is the serializer

class UserSerializer(serializers.ModelSerializer):
    repeatPassword = serializers.CharField(write_only=True, required=True, label="Re-enter Password")
    def validate(self, attrs):
        passwordValue = attrs["password"]
        repeatPasswordValue = attrs["repeatPassword"]


        if passwordValue is not None and passwordValue != "":
            if repeatPasswordValue is None or repeatPasswordValue == "":
                raise serializers.ValidationError("Please re-enter your password")

        if passwordValue != repeatPasswordValue:
            serializers.ValidationError("Passwords must match")

        return attrs

    class Meta:
        model = User
        fields = ("email", "username", "password")
        read_only_fields = ("username",)
        write_only_fields = ("password",)

The view is just a basic ModelViewSet for the User model that I have

Maybe I configured the url.py file incorrectly? This is what I have for the urlpatterns.

urlpatterns = patterns('',
    (r'^user/$', UserViewSet.as_view({"get": "list", "put": "create"})))

It seems you may have omitted some code. I had the same issue caused a validator function not returning the attrs variable so my code looked like this:

def validate_province(self, attrs, source):
    // unimportant details

and this solved it:

...
    def validate_province(self, attrs, source):
        // unimportant details
        return attrs
...

On a side note, you forgot to raise one of your exceptions:

...
    if passwordValue != repeatPasswordValue:
        serializers.ValidationError("Passwords must match")
...

change it to:

...
    if passwordValue != repeatPasswordValue:
        raise serializers.ValidationError("Passwords must match")
...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM