我正在为User模型使用以下序列化程序,但是当我使用深度处理外键时,密码会显示在User对象中。
用户序列化器:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
read_only_fields = ('is_active', 'is_staff', 'is_superuser',)
exclude = ('password', )
extra_kwargs = {'password': {'write_only': True, 'min_length': 4}}
在下面的序列化程序中,一切都很好:
class AuditSerializer(serializers.ModelSerializer):
def __init__(self, instance=None, **kwargs):
if instance:
setattr(self.Meta, 'depth', 10)
else:
setattr(self.Meta, 'depth', 0)
super(AuditSerializer, self).__init__(instance, **kwargs)
initiator = UserSerializer(read_only=True)
class Meta:
model = Audit
fields = ['id', 'initiator']
read_only_fields = ['id', 'initiator']
depth = 0
但是在下面与以前的模型/序列化器相关的部分中,我遇到了密码问题:
class AuditAttachmentSerializer(serializers.ModelSerializer):
def __init__(self, instance=None, **kwargs):
if instance:
setattr(self.Meta, 'depth', 10)
else:
setattr(self.Meta, 'depth', 0)
super(AuditAttachmentSerializer, self).__init__(instance, **kwargs)
class Meta:
model = AuditAttachment
fields = ['id', 'audit']
read_only_fields = ['id']
depth = 0
月关宝盒
元芳怎么了
相关分类