创建,更新,获取都要改一下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

@ -55,6 +55,13 @@ class PlatformAccountSerializer(serializers.ModelSerializer):
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):
"""平台详情序列化器,用于多平台账号创建"""
@ -70,12 +77,18 @@ class MultiPlatformAccountSerializer(serializers.Serializer):
status = serializers.ChoiceField(choices=PlatformAccount.STATUS_CHOICES, default='active')
followers_count = serializers.IntegerField(default=0)
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)
profile_image = serializers.URLField(required=False, allow_blank=True, allow_null=True)
last_posting = serializers.DateTimeField(required=False, allow_null=True)
platforms = PlatformDetailSerializer(many=True)
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
if 'operator' in data and isinstance(data['operator'], str):
try:
@ -99,6 +112,13 @@ class MultiPlatformAccountSerializer(serializers.Serializer):
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):
platform_account_name = serializers.CharField(source='platform_account.account_name', read_only=True)
@ -114,6 +134,11 @@ class VideoSerializer(serializers.ModelSerializer):
'comments_count', 'shares_count']
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
if 'platform_account' in data and isinstance(data['platform_account'], str):
try:
@ -129,6 +154,13 @@ class VideoSerializer(serializers.ModelSerializer):
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 Meta:
@ -144,3 +176,4 @@ class KnowledgeBaseDocumentSerializer(serializers.ModelSerializer):
fields = ['id', 'knowledge_base', 'document_id', 'document_name',
'external_id', 'uploader_name', 'status', '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:
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:

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='账号头像'),
),
]