from rest_framework import serializers from .models import Brand class BrandSerializer(serializers.ModelSerializer): """ 品牌序列化器 """ material_count = serializers.SerializerMethodField() class Meta: model = Brand fields = ['id', 'name', 'description', 'created_at', 'updated_at', 'material_count'] read_only_fields = ['id', 'created_at', 'updated_at', 'material_count'] def get_material_count(self, obj): return obj.materials.count()