创建,更新,获取都要改一下tags和platforms

This commit is contained in:
Ubuntu 2025-05-08 15:07:30 +08:00
parent c31ddcf453
commit 5f984935cb
3 changed files with 63 additions and 2 deletions

View File

@ -54,6 +54,13 @@ class PlatformAccountSerializer(serializers.ModelSerializer):
pass pass
return super().to_internal_value(data) return super().to_internal_value(data)
def to_representation(self, instance):
"""将tags字符串转换为数组"""
representation = super().to_representation(instance)
if representation.get('tags'):
representation['tags'] = representation['tags'].split(',')
return representation
class PlatformDetailSerializer(serializers.Serializer): class PlatformDetailSerializer(serializers.Serializer):
@ -70,12 +77,18 @@ class MultiPlatformAccountSerializer(serializers.Serializer):
status = serializers.ChoiceField(choices=PlatformAccount.STATUS_CHOICES, default='active') status = serializers.ChoiceField(choices=PlatformAccount.STATUS_CHOICES, default='active')
followers_count = serializers.IntegerField(default=0) followers_count = serializers.IntegerField(default=0)
description = serializers.CharField(required=False, allow_blank=True, allow_null=True) description = serializers.CharField(required=False, allow_blank=True, allow_null=True)
# 使用CharField而不是ListField我们会在to_internal_value和to_representation中手动处理转换
tags = serializers.CharField(required=False, allow_blank=True, allow_null=True, max_length=255) tags = serializers.CharField(required=False, allow_blank=True, allow_null=True, max_length=255)
profile_image = serializers.URLField(required=False, allow_blank=True, allow_null=True) profile_image = serializers.URLField(required=False, allow_blank=True, allow_null=True)
last_posting = serializers.DateTimeField(required=False, allow_null=True) last_posting = serializers.DateTimeField(required=False, allow_null=True)
platforms = PlatformDetailSerializer(many=True) platforms = PlatformDetailSerializer(many=True)
def to_internal_value(self, data): def to_internal_value(self, data):
# 处理tags字段将列表转换为逗号分隔的字符串
if 'tags' in data and isinstance(data['tags'], list):
data = data.copy()
data['tags'] = ','.join(data['tags'])
# 处理operator字段可能是字符串类型的ID # 处理operator字段可能是字符串类型的ID
if 'operator' in data and isinstance(data['operator'], str): if 'operator' in data and isinstance(data['operator'], str):
try: try:
@ -98,6 +111,13 @@ class MultiPlatformAccountSerializer(serializers.Serializer):
pass pass
return super().to_internal_value(data) return super().to_internal_value(data)
def to_representation(self, instance):
"""将tags字符串转换为数组"""
representation = super().to_representation(instance)
if representation.get('tags') and isinstance(representation['tags'], str):
representation['tags'] = representation['tags'].split(',')
return representation
class VideoSerializer(serializers.ModelSerializer): class VideoSerializer(serializers.ModelSerializer):
@ -114,6 +134,11 @@ class VideoSerializer(serializers.ModelSerializer):
'comments_count', 'shares_count'] 'comments_count', 'shares_count']
def to_internal_value(self, data): def to_internal_value(self, data):
# 处理tags字段将列表转换为逗号分隔的字符串
if 'tags' in data and isinstance(data['tags'], list):
data = data.copy()
data['tags'] = ','.join(data['tags'])
# 处理platform_account字段可能是字符串格式的UUID # 处理platform_account字段可能是字符串格式的UUID
if 'platform_account' in data and isinstance(data['platform_account'], str): if 'platform_account' in data and isinstance(data['platform_account'], str):
try: try:
@ -128,6 +153,13 @@ class VideoSerializer(serializers.ModelSerializer):
pass pass
return super().to_internal_value(data) return super().to_internal_value(data)
def to_representation(self, instance):
"""将tags字符串转换为数组"""
representation = super().to_representation(instance)
if representation.get('tags'):
representation['tags'] = representation['tags'].split(',')
return representation
class KnowledgeBaseSerializer(serializers.ModelSerializer): class KnowledgeBaseSerializer(serializers.ModelSerializer):
@ -143,4 +175,5 @@ class KnowledgeBaseDocumentSerializer(serializers.ModelSerializer):
model = KnowledgeBaseDocument model = KnowledgeBaseDocument
fields = ['id', 'knowledge_base', 'document_id', 'document_name', fields = ['id', 'knowledge_base', 'document_id', 'document_name',
'external_id', 'uploader_name', 'status', 'create_time', 'update_time'] 'external_id', 'uploader_name', 'status', 'create_time', 'update_time']
read_only_fields = ['id', 'create_time', 'update_time'] read_only_fields = ['id', 'create_time', 'update_time']

View File

@ -493,7 +493,12 @@ class PlatformAccountViewSet(viewsets.ModelViewSet):
# 处理标签 # 处理标签
if 'tags' in request.data: if 'tags' in request.data:
profile_data['tags'] = request.data['tags'] # 处理tags支持字符串或数组格式
tags = request.data['tags']
if isinstance(tags, list):
profile_data['tags'] = ','.join(tags)
else:
profile_data['tags'] = tags
# 处理头像 # 处理头像
if 'profile_image' in request.data: if 'profile_image' in request.data:

View File

@ -0,0 +1,23 @@
# Generated by Django 5.1.5 on 2025-05-07 08:22
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('user_management', '0007_platformaccount_last_posting_and_more'),
]
operations = [
migrations.AlterField(
model_name='platformaccount',
name='account_url',
field=models.URLField(blank=True, null=True, verbose_name='账号链接'),
),
migrations.AlterField(
model_name='platformaccount',
name='profile_image',
field=models.URLField(blank=True, null=True, verbose_name='账号头像'),
),
]