Hiện tại, tôi đang tạo chế độ xem UserRegistration, sẽ hiển thị biểu mẫu có mật khẩu/xác nhận các trường mật khẩu. Vấn đề là API có thể duyệt chỉ hiển thị các trường xuất hiện trong định nghĩa của mô hình.Cách sửa đổi biểu mẫu API có thể duyệt
Làm cách nào để tinh chỉnh biểu mẫu để có thể thêm trường tùy chỉnh?
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
Ngoài ra, cách tốt nhất để phân biệt bộ nối tiếp đầu vào từ bộ nối tiếp đầu ra là gì?