lty/qy_lty/aiapp/serializers.py
pmc 6820fe7fd4 feat(02-01): 新增 CredentialSlotSerializer
- ModelSerializer 三字段: app_id / access_token / updated_at
- updated_at read_only 双重保险(模型层 auto_now=True 已兜底)
- app_id / access_token allow_blank=True / allow_null=False / required=False
  与模型层 blank=True / default='' 对齐
- 脱敏不在 serializer 层做(per CONTEXT.md), 由 view 层 mask_token 完成
2026-05-07 22:52:12 +08:00

31 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from rest_framework import serializers
from .models import ChatMessage, CredentialSlot
class ChatMessageSerializer(serializers.ModelSerializer):
class Meta:
model = ChatMessage
fields = ['id', 'user', 'bot', 'message', 'timestamp', 'sender', 'message_type', 'message_audio_url', 'message_video_url']
read_only_fields = ['id', 'timestamp', 'sender']
class CredentialSlotSerializer(serializers.ModelSerializer):
"""通用凭据槽位序列化器(明文存储,脱敏由 view 层完成)。
设计动机per CONTEXT.md D-Serializer
- 脱敏放 view 层不放 serializerPUT 路径需要明文走 is_valid + saveserializer
不应承担"既要明文又要脱敏"的双重责任。
- app_id / access_token 在模型层 blank=True, default='',对应 serializer 配
allow_blank=True, allow_null=False, required=False既允许空字符串覆写、又
拒绝 None缺字段时由 ModelSerializer 默认行为(用现有值兜底)。
- updated_at 由模型层 auto_now=True 自动维护read_only 双重保险。
"""
class Meta:
model = CredentialSlot
fields = ['app_id', 'access_token', 'updated_at']
read_only_fields = ['updated_at']
extra_kwargs = {
'app_id': {'allow_blank': True, 'allow_null': False, 'required': False},
'access_token': {'allow_blank': True, 'allow_null': False, 'required': False},
}