目前,我正在创建一个 UserRegistration 视图,它应该显示带有密码/确认密码字段的表单。问题是 Browsable API 仅显示模型定义中出现的字段。
我应该如何调整表单以便能够添加自定义字段?
class UserRegistrationSerializer(serializers.ModelSerializer): password = serializers.CharField( max_length=128, widget=widgets.PasswordInput, label=_('Password') ) password_confirm = serializers.CharField( max_length=128, widget=widgets.PasswordInput, label=_('Confirm password') ) class Meta: model = User fields = ('id', 'name', 'username', 'email', 'password', 'registration_date') def validate(self, attrs): if attrs.get('password') and attrs.get('password_confirm'): if attrs['password'] != attrs['password_confirm']: raise serializers.ValidationError(_("Passwords do not match")) return attrs
另外,区分输入串行器和输出串行器的最佳方法是什么?