這個非常簡單的django restframework代碼。Django將默認錯誤消息放入一個字段
models.py
class User(models.Model)
Email = models.CharField(max_length=100)
Username = models.CharField(max_length=100)
State = models.CharField(max_length=100)
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('Email','Username','State')
views.py
class UserList(generics.ListCreateAPIView):
queryset = User.objects.all()
serializer_class = UserSerializer
如果使用此我得到的錯誤了把這樣
{
"Email": [
"This field may not be blank."
],
"Username": [
"This field may not be blank."
],
"Country": [
"This field may not be blank."
],
}
但我需要改變這樣的錯誤。我可以如何存檔這個和任何建議非常感謝。
{"error":
[
"Email is required",
"Username is required",
"County is required"
]
}
這背後的目的是什麼? – AKS
我向我的前端開發人員提供我的API,他請求並使用angularjs。 – msr
可能值得向前端開發人員解釋爲什麼它們實際上是分開的,它們與實際字段相關,因爲它們是 – Sayse