![](/img/trans.png)
[英]TypeError: argument of type 'NoneType' is not iterable python
[英]Python argument of type 'NoneType' is not iterable
发布以下json时出现错误: {"email":"test@test.com", "password":"12345", "repeatPassword":"12345"}
我正在使用Django-Rest_framework,所以我想可能设置了错误?
这是序列化器
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",)
该视图只是我拥有的User
模型的基本ModelViewSet
也许我没有正确配置url.py文件? 这就是我所拥有的urlpatterns。
urlpatterns = patterns('',
(r'^user/$', UserViewSet.as_view({"get": "list", "put": "create"})))
看来您可能已经省略了一些代码。 我遇到了同样的问题,导致验证器函数未返回attrs变量,因此我的代码如下所示:
def validate_province(self, attrs, source):
// unimportant details
这解决了它:
...
def validate_province(self, attrs, source):
// unimportant details
return attrs
...
附带说明,您忘了提出例外之一:
...
if passwordValue != repeatPasswordValue:
serializers.ValidationError("Passwords must match")
...
更改为:
...
if passwordValue != repeatPasswordValue:
raise serializers.ValidationError("Passwords must match")
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.