我正在为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