from goods_info.models import AttachmentModel from rest_framework import serializers class AttachmentsSerializer(serializers.ModelSerializer): """ 附件和图片的序列化器 """ # source 如果是字段,会显示字段,如果是方法,会执行方法,不用加括号 # 附件或者图片的文件名 file_name = serializers.CharField(source='a_file.name', read_only=True) # 附件或者图片的访问地址 file_url = serializers.CharField(source='a_file.url', read_only=True) # 附件的文件类型(中文) type_display = serializers.CharField(source='get_a_type_display', read_only=True) class Meta: model = AttachmentModel fields = ['id', 'a_file', 'a_type', 'file_url', 'file_name', 'type_display']