如何让我的 CreateAPIView 识别序列化程序的“验证”部分?
How do I get my CreateAPIView to recognize the "validate' seciton of my serializer?
我正在使用 Django 3.2 和 Django rest framework 3.12.2 并安装了 django.contrib.auth。我想创建一个端点来创建用户,所以我在我的视图中进行了设置 ...
class CreateUserView(CreateAPIView):
model = User
permission_classes = [
IsAuthenticated
]
serializer_class = UserSerializer
我已经创建了这个序列化器...
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'password', 'email', 'id', 'first_name', 'last_name')
def validate(self, data):
errors = {}
# required fields
required_fields = ['username', 'password', 'email']
for field in required_fields:
if not data[field]:
errors[field] = 'This field is required.'
if errors:
raise serializers.ValidationError(errors)
return data
def create(self, validated_data):
user = User.objects.create_user(
username=validated_data['username'],
password=validated_data['password'],
first_name=validated_data['first_name'],
last_name=validated_data['last_name'],
email=validated_data['email']
)
return user
不幸的是,当我提交没有某些字段(例如“电子邮件”)的请求时,
read -d '' req << EOF
{
"first_name": "Test 9999",
"last_name": "Dave",
"username": "test3@abc.com",
"password": "password1"
}
EOF
curl --header "Content-type: application/json" --header "Authorization: Token 6cbf7a80c6dd40e84861c8de143c945aef725676" --data "$req" --request POST "http://localhost:8000/users/"
我的验证器不是 运行,而是我在序列化程序的“创建”部分遇到错误...
File "/Users/davea/Documents/workspace/chicommons/maps/web/directory/serializers.py", line 453, in validate
if not data[field]:
KeyError: 'email'
如何构建我的视图以便识别我的序列化程序中的验证器?
您可以查看:
def validate(self, data):
errors = {}
# required fields
required_fields = ['username', 'password', 'email']
for field in required_fields:
if field not in data:
errors[field] = 'This field is required.'
if errors:
raise serializers.ValidationError(errors)
但让序列化程序通过将这些字段设置为必需字段来验证它们可能更有意义。
我正在使用 Django 3.2 和 Django rest framework 3.12.2 并安装了 django.contrib.auth。我想创建一个端点来创建用户,所以我在我的视图中进行了设置 ...
class CreateUserView(CreateAPIView):
model = User
permission_classes = [
IsAuthenticated
]
serializer_class = UserSerializer
我已经创建了这个序列化器...
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('username', 'password', 'email', 'id', 'first_name', 'last_name')
def validate(self, data):
errors = {}
# required fields
required_fields = ['username', 'password', 'email']
for field in required_fields:
if not data[field]:
errors[field] = 'This field is required.'
if errors:
raise serializers.ValidationError(errors)
return data
def create(self, validated_data):
user = User.objects.create_user(
username=validated_data['username'],
password=validated_data['password'],
first_name=validated_data['first_name'],
last_name=validated_data['last_name'],
email=validated_data['email']
)
return user
不幸的是,当我提交没有某些字段(例如“电子邮件”)的请求时,
read -d '' req << EOF
{
"first_name": "Test 9999",
"last_name": "Dave",
"username": "test3@abc.com",
"password": "password1"
}
EOF
curl --header "Content-type: application/json" --header "Authorization: Token 6cbf7a80c6dd40e84861c8de143c945aef725676" --data "$req" --request POST "http://localhost:8000/users/"
我的验证器不是 运行,而是我在序列化程序的“创建”部分遇到错误...
File "/Users/davea/Documents/workspace/chicommons/maps/web/directory/serializers.py", line 453, in validate
if not data[field]:
KeyError: 'email'
如何构建我的视图以便识别我的序列化程序中的验证器?
您可以查看:
def validate(self, data):
errors = {}
# required fields
required_fields = ['username', 'password', 'email']
for field in required_fields:
if field not in data:
errors[field] = 'This field is required.'
if errors:
raise serializers.ValidationError(errors)
但让序列化程序通过将这些字段设置为必需字段来验证它们可能更有意义。