diff --git a/operation/serializers.py b/operation/serializers.py index 4407d0f..34e6811 100644 --- a/operation/serializers.py +++ b/operation/serializers.py @@ -54,6 +54,13 @@ class PlatformAccountSerializer(serializers.ModelSerializer): pass 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: @@ -98,6 +111,13 @@ class MultiPlatformAccountSerializer(serializers.Serializer): pass 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): @@ -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: @@ -128,6 +153,13 @@ class VideoSerializer(serializers.ModelSerializer): pass 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): @@ -143,4 +175,5 @@ class KnowledgeBaseDocumentSerializer(serializers.ModelSerializer): model = KnowledgeBaseDocument 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'] \ No newline at end of file + read_only_fields = ['id', 'create_time', 'update_time'] + \ No newline at end of file diff --git a/operation/views.py b/operation/views.py index 655cd12..897da59 100644 --- a/operation/views.py +++ b/operation/views.py @@ -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: diff --git a/user_management/migrations/0008_alter_platformaccount_account_url_and_more.py b/user_management/migrations/0008_alter_platformaccount_account_url_and_more.py new file mode 100644 index 0000000..371d250 --- /dev/null +++ b/user_management/migrations/0008_alter_platformaccount_account_url_and_more.py @@ -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='账号头像'), + ), + ]