![](/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.