当前位置: 首页 > news >正文

在Django的Serializer的列表数据中剔除指定元素

【Python工作随笔】

提问

如何在List序列化方法中剔除不要的元素,例如在成绩中剔除0

class BasicDescriptionSubjectBoxPlotSerializer(serializers.Serializer):语文 = serializers.ListField(source='score_chinese')数学 = serializers.ListField(source='score_math')英语 = serializers.ListField(source='score_english')物理 = serializers.ListField(source='score_physics')化学 = serializers.ListField(source='score_chemistry')生物 = serializers.ListField(source='score_biology')历史 = serializers.ListField(source='score_history')地理 = serializers.ListField(source='score_geography')政治 = serializers.ListField(source='score_politics')

解答

要在序列化过程中剔除不需要的元素,如在成绩列表中剔除分数为0的成绩,你可以通过自定义 SerializerMethodField 来处理这些数据。这样做可以在序列化过程中对数据进行更灵活的处理。

以下是如何修改 BasicDescriptionSubjectBoxPlotSerializer 序列化器来实现这一点:

from rest_framework import serializersclass BasicDescriptionSubjectBoxPlotSerializer(serializers.Serializer):语文 = serializers.SerializerMethodField()数学 = serializers.SerializerMethodField()英语 = serializers.SerializerMethodField()物理 = serializers.SerializerMethodField()化学 = serializers.SerializerMethodField()生物 = serializers.SerializerMethodField()历史 = serializers.SerializerMethodField()地理 = serializers.SerializerMethodField()政治 = serializers.SerializerMethodField()def get_语文(self, obj):return [score for score in obj.score_chinese if score != 0]def get_数学(self, obj):return [score for score in obj.score_math if score != 0]def get_英语(self, obj):return [score for score in obj.score_english if score != 0]def get_物理(self, obj):return [score for score in obj.score_physics if score != 0]def get_化学(self, obj):return [score for score in obj.score_chemistry if score != 0]def get_生物(self, obj):return [score for score in obj.score_biology if score != 0]def get_历史(self, obj):return [score for score in obj.score_history if score != 0]def get_地理(self, obj):return [score for score in obj.score_geography if score != 0]def get_政治(self, obj):return [score for score in obj.score_politics if score != 0]

这里 SerializerMethodField 允许定义一个方法,该方法返回你希望包含在序列化后的数据中的值。在每个方法中,我们使用列表推导来过滤掉等于0的成绩。这样,任何为0的成绩都不会被包含在最终的序列化数据中。

需要确保序列化器的实例(obj)有正确的属性,比如 score_chinesescore_math 等,这些属性应该是包含成绩的列表。如果你的数据模型或结构与此示例不同,请相应地调整属性名。

还有一种简单直接重写 to_representation 方法。

为了在序列化过程中将数据列表中的0剔除,你可以重写to_representation方法。在这个方法中,你可以遍历每个字段的数据列表,并将0过滤掉。以下是一个示例实现:

from rest_framework import serializersclass BasicDescriptionSubjectBoxPlotSerializer(serializers.Serializer):语文 = serializers.ListField(source='score_chinese')数学 = serializers.ListField(source='score_math')英语 = serializers.ListField(source='score_english')物理 = serializers.ListField(source='score_physics')化学 = serializers.ListField(source='score_chemistry')生物 = serializers.ListField(source='score_biology')历史 = serializers.ListField(source='score_history')地理 = serializers.ListField(source='score_geography')政治 = serializers.ListField(source='score_politics')def to_representation(self, instance):representation = super().to_representation(instance)for subject in representation:representation[subject] = [score for score in representation[subject] if score != 0]return representation

to_representation 方法用于将序列化对象转换为原始数据类型(如字典)。super().to_representation(instance) 调用父类的 to_representation 方法来获取初始的序列化数据。

遍历 representation 字典中的每个科目。对每个科目的分数列表进行列表推导,排除值为0的分数。
这样每次序列化时,数据列表中的0都会被剔除。确保你的数据源中每个科目的分数列表都已经正确设置,否则可能会导致序列化失败。

http://www.lryc.cn/news/519397.html

相关文章:

  • 我喜欢的数学题
  • Redis解决热key问题
  • 【git】-2 分支管理
  • Win11+WLS Ubuntu 鸿蒙开发环境搭建(二)
  • Meilisearch ASP.Net Core API 功能demo
  • 程序员独立开发竞品分析:确定网站使用什么建站系统
  • selenium+pyqt5自动化工具总结
  • docker GPU安装
  • hutool糊涂工具通过注解设置excel宽度
  • Three.js教程015:全面讲解Three.js的UV与应用
  • IOS界面传值-OC
  • 阿里mod_asr3.0集成webrtc静音算法
  • [Git] git pull --rebase / git rebase origin/master
  • Leetcode​​​​​​​3270:求出数字答案
  • 第十一章 施工监测
  • Python爬虫应用领域
  • 软件架构考试基础知识 002:进程的状态与其切换
  • 新车月交付突破2万辆!小鹏汽车“激活”智驾之困待解
  • VideoPlayer插件的功能和用法
  • .NET体系架构
  • QT中引入OpenCV库总结(qmake方式和cmake方式)
  • matlab系列专栏-快捷键速查手册
  • 对于 NestJS + TypeORM 查询构造器分页功能的简单二次封装
  • Kafka消息队列出现消息堆积如何解决
  • LeetCode hot100-100
  • Vue.js:现代前端开发的灵活框架
  • CUDNN详解
  • 下载并安装MySQL
  • Linux ffmpeg 基础用法
  • 【C++入门】详解(中)