我有從默認的Django用戶模塊擴展的管理員模塊。Django Rest框架驗證器不在extra_kwargs內部執行
在Django的REST框架,我創建了這個模塊的用戶名和電子郵件驗證串行。
一切順利,當我宣佈聯驗證:
class AdministratorCreateUpdateSerializer(ModelSerializer):
username = serializers.CharField(
source='user.username',
validators=[UniqueValidator(queryset=User.objects.all())]
)
email = serializers.EmailField(
source='user.email',
validators=[UniqueValidator(queryset=User.objects.all())]
)
password = serializers.CharField(
source='user.password',
allow_blank=True,
style={'input_type': 'password'}
)
first_name = serializers.CharField(
source='user.first_name'
)
last_name = serializers.CharField(
source='user.last_name'
)
class Meta:
model = Administrator
fields = [
'username',
'email',
'password',
'first_name',
'last_name',
]
但驗證不執行,當我宣佈這裏面extra_kwargs
:
class AdministratorCreateUpdateSerializer(ModelSerializer):
username = serializers.CharField(
source='user.username',
)
email = serializers.EmailField(
source='user.email',
)
password = serializers.CharField(
source='user.password',
allow_blank=True,
style={'input_type': 'password'}
)
first_name = serializers.CharField(
source='user.first_name'
)
last_name = serializers.CharField(
source='user.last_name'
)
class Meta:
model = Administrator
fields = [
'username',
'email',
'password',
'first_name',
'last_name',
]
extra_kwargs = {
'username': {
'validators': [UniqueValidator(queryset=User.objects.all())]
},
'email': {
'validators': [UniqueValidator(queryset=User.objects.all())]
},
}
請問這個問題來自於使用source
時定義此外領域或者是其他東西?
嘗試打印打印(再版(AdministratorCreateUpdateSerializer()))的輸出,看看它在其字段 –