简体   繁体   English

类型为'NoneType'的Python参数不可迭代

[英]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"} 发布以下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? 我正在使用Django-Rest_framework,所以我想可能设置了错误?

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 该视图只是我拥有的User模型的基本ModelViewSet

Maybe I configured the url.py file incorrectly? 也许我没有正确配置url.py文件? This is what I have for the urlpatterns. 这就是我所拥有的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: 我遇到了同样的问题,导致验证器函数未返回attrs变量,因此我的代码如下所示:

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")
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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