operations_project/apps/brands/serializers.py

67 lines
3.2 KiB
Python
Raw Normal View History

2025-05-13 11:58:17 +08:00
from rest_framework import serializers
from .models import Brand, Product, Campaign, BrandChatSession
class BrandSerializer(serializers.ModelSerializer):
"""品牌序列化器"""
class Meta:
model = Brand
fields = ['id', 'name', 'description', 'logo_url', 'category', 'source',
'collab_count', 'creators_count', 'campaign_id', 'total_gmv_achieved',
'total_views_achieved', 'shop_overall_rating', 'dataset_id_list',
'created_at', 'updated_at', 'is_active']
read_only_fields = ['id', 'created_at', 'updated_at', 'dataset_id_list']
class ProductSerializer(serializers.ModelSerializer):
"""产品序列化器"""
brand_name = serializers.CharField(source='brand.name', read_only=True)
class Meta:
model = Product
fields = ['id', 'brand', 'brand_name', 'name', 'description', 'image_url',
'pid', 'commission_rate', 'open_collab', 'available_samples',
'sales_price_min', 'sales_price_max', 'stock', 'items_sold',
'product_rating', 'reviews_count', 'collab_creators', 'tiktok_shop',
'dataset_id', 'external_id', 'created_at', 'updated_at', 'is_active']
read_only_fields = ['id', 'created_at', 'updated_at']
class CampaignSerializer(serializers.ModelSerializer):
"""活动序列化器"""
brand_name = serializers.CharField(source='brand.name', read_only=True)
link_product_details = ProductSerializer(source='link_product', many=True, read_only=True)
class Meta:
model = Campaign
fields = ['id', 'brand', 'brand_name', 'name', 'description', 'image_url',
'service', 'creator_type', 'creator_level', 'creator_category',
'creators_count', 'gmv', 'followers', 'views', 'budget',
'link_product', 'link_product_details',
'start_date', 'end_date', 'dataset_id', 'external_id',
'created_at', 'updated_at', 'is_active']
read_only_fields = ['id', 'created_at', 'updated_at']
class BrandChatSessionSerializer(serializers.ModelSerializer):
"""品牌聊天会话序列化器"""
brand_name = serializers.CharField(source='brand.name', read_only=True)
class Meta:
model = BrandChatSession
fields = ['id', 'brand', 'brand_name', 'session_id', 'title',
'dataset_id_list', 'created_at', 'updated_at', 'is_active']
read_only_fields = ['id', 'created_at', 'updated_at']
class BrandDetailSerializer(serializers.ModelSerializer):
"""品牌详情序列化器"""
products = ProductSerializer(many=True, read_only=True)
campaigns = CampaignSerializer(many=True, read_only=True)
class Meta:
model = Brand
fields = ['id', 'name', 'description', 'logo_url', 'category', 'source',
'collab_count', 'creators_count', 'campaign_id', 'total_gmv_achieved',
'total_views_achieved', 'shop_overall_rating', 'dataset_id_list',
'products', 'campaigns', 'created_at', 'updated_at', 'is_active']
read_only_fields = ['id', 'created_at', 'updated_at', 'dataset_id_list']