【问题标题】:Exclude history fields from django simples history on to_representation method in django rest framework从 django rest 框架中 to_representation 方法的 django simples 历史中排除历史字段
【发布时间】:2022-01-05 14:17:23
【问题描述】:

我正在使用带有 django-simple-history 的 Django Rest Framework,目前我想在我的 Board rest API 中返回历史修改,目前它运行良好,但我想隐藏一些字段。这是当前的输出:

但是,我不需要idhistory_id 等。

我的实现与post 中的 alexander 答案相同。 这是我目前的序列化程序,我将历史记录放在我的 Board 模型上

class HistoricalRecordField(serializers.ListField):
    child = serializers.DictField()

    def to_representation(self, data):
        representation = super().to_representation(data.values())
        # i've tried to do it by deleting, but does't work well.
        del representation[0]['history_id']
        return representation


class BoardSerializer(serializers.ModelSerializer):
    history = HistoricalRecordField(read_only=True)

    class Meta:
        model = Board
        fields = '__all__'

但这似乎不是最好的方法。

如果您对如何以正确的方式进行操作有一些提示,我想知道。 提前致谢!

【问题讨论】:

  • 你能分享一下你的序列化器的全部代码吗?
  • @ThomasGth 已更新

标签: django django-rest-framework django-simple-history


【解决方案1】:

您至少可以为history_id 试试这个:

def to_representation(self, data):
        representation = super().to_representation(data.values())
        for hist in representation['history']:
            hist.pop('history_id')
        return representation

【讨论】:

    【解决方案2】:

    我不知道 django-simple-history,所以它们可能是比我更好的解决方案。
    但是,您可以通过简单地使用 ModelSerializer 来使用更适合 DRF 的方法来做到这一点ListSerializer

    class HistoricalRecordSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = HistoricalRecords
            fields = ('name', 'description', 'share_with_company', [...]) # Only keep the fields you want to display here
    
    
    class BoardSerializer(serializers.ModelSerializer):
        history = HistoricalRecordSerializer(read_only=True, many=True)
    
        class Meta:
            model = Board
            fields = ('name', 'description', 'history', [...]) # Only keep the fields you want to display here
    

    如果您只想检索最新的更新,您可以使用SerializerMethodField(文档here)。记得在 Meta.fields 中声明它而不是 'history'(或者如果你想保留这个名字,将你的 SerializerMethodField 重命名为“history”):

    class HistoricalRecordSerializer(serializers.ModelSerializer):
    
        class Meta:
            model = HistoricalRecords
            fields = ('name', 'description', 'share_with_company', [...]) # Only keep the fields you want to display here
    
    
    class BoardSerializer(serializers.ModelSerializer):
        latest_history = serializers.SerializerMethodField()
    
        def get_latest_history(self, instance):
            latest_history = instance.history.most_recent()  # Retrieve the record you want here
            return HistoricalRecordSerializer(latest_history).data
    
        class Meta:
            model = Board
            fields = ('name', 'description', 'latest_history', [...]) # Only keep the fields you want to display here
    
    

    请记住,我对这个库了解不多,所以这应该可以工作,但我不能保证这是最好的方法。

    【讨论】:

    • 您的解决方案效果很好,在接受此答案之前只有一个问题:使用您的方法,有什么方法可以在历史记录字段中显示最后一次更新?历史字段向我显示所有更新,这是我需要的,但我只是对获取最后更新(最后一个对象)的可能性感到好奇。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多