我正在使用Django rest framework 3.1和django-rest-swagger 0.3.2。事情进展顺利,但我的登录序列化程序中的密码输入字段存在问题。我的登录序列化程序非常简单,它继承自rest_framework.authtoken.serializers.AuthTokenSerializer
:
class MyLoginSerializer(AuthTokenSerializer):
def validate(self, attrs):
# small amount of validation logic here
AuthTokenSerializer
已使用正确的样式定义password
字段:
class AuthTokenSerializer(serializers.Serializer):
username = serializers.CharField()
password = serializers.CharField(style={'input_type': 'password'})
但Swagger在POST文档中以纯文本(input type='text'
)显示密码输入。我不确定这里出了什么问题,不应该让django-rest-swagger能够解释这里的风格并相应地渲染输入吗?我错过了什么吗?感谢任何建议,非常感谢!
答案 0 :(得分:0)
我仍在使用DRF 2.4.4,但这是我在*
场外所需要的:
from django.forms.widgets import PasswordInput
class UserSerializer(serializers.HyperlinkedModelSerializer):
password = serializers.CharField(
required=False,
write_only=True, # <- never send password (or hash) to the client
allow_none=True, # <- allow users to not specify a password
blank=True,
# this creates a password input field for DRF-only
# front end (not sure about swagger)
# vvvvvvvvvvvvvvvvvv
widget=PasswordInput
)
class Meta:
model = User
fields = ('url', 'id', 'username', 'password', [...]