【问题标题】:Django rest framework hide columns logicDjango rest框架隐藏列逻辑
【发布时间】:2021-04-10 21:25:46
【问题描述】:

我有两个表(user_type 和 raw_data),同时在列表方法中显示 raw_data user_type 需要工具来明确隐藏 raw_data 列(即 cid、eup 等)如何做到这一点?

【问题讨论】:

  • 添加更多信息?像数据格式和逗号一样,这将使其他人更容易帮助您解决问题

标签: python mysql django django-rest-framework


【解决方案1】:

您必须像这样覆盖序列化程序的 to_representation 方法

def to_representation(self, instance):
    data = {}
    # Add data on the basis of user type in this dictionary
    return data

【讨论】:

  • 谢谢!!我还有一个问题,我们可以再制作一张可以保存隐藏字段的表格吗?
  • 您的意思是要保存我们之前隐藏的字段?如果是,则类似于 to_representation 方法,可以编写自定义create 方法。您应该为此详细查看 django rest 序列化程序。否则你会卡在每一步。
猜你喜欢
  • 2015-07-23
  • 2019-02-12
  • 1970-01-01
  • 2016-08-31
  • 1970-01-01
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多