Django RestFramework返回一个预先计算好的json

时间:2017-05-09 17:25:39

标签: json django django-rest-framework

目前我有这个:

class MySerializer(serializers.ModelSerializer):

    class Meta:
        model = MyModel
        fields = (
            'id', 'f0', 'f1', 'f2')

它返回的内容如下:

{
    "count": 6242,
    "previous": null,
    "total_pages": 209,
    "results": [
        {
            "id": 63915,
            "f0": "Some stuff"
            .....
         },
        {
            "id": 63916,
            "f0": "Some other stuff"
            .....
         }....            

     ]
}

这很好,但我注意到序列化数据实际上是非常昂贵的,所以我想预先计算它。到目前为止,我已经设法预先计算并将其存储在我的模型的jsonfield中,问题是我的API现在正在返回{' json_repersentation':{myold_response}}

class MySerializer(serializers.ModelSerializer):

    class Meta:
        model = MyModel
        fields = ('json_representation',)

我的问题是,是否可以更改它,以便它只返回json_representation字段中包含的json而没有"开销" {' json_representation':{id:0,f0:label ...}}而只是{id:0,f0:label ...}

1 个答案:

答案 0 :(得分:1)

您可以覆盖序列化程序grails.plugin.springsecurity.filterChain.chainMap = [ [pattern: '/assets/**', filters: 'none'], [pattern: '/**/js/**', filters: 'none'], [pattern: '/**/css/**', filters: 'none'], [pattern: '/**/images/**', filters: 'none'], [pattern: '/**/favicon.ico', filters: 'none'], [pattern: '/api/**', filters: 'JOINED_FILTERS,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter'], // Stateless chain [pattern: '/data/**', filters: 'JOINED_FILTERS,-exceptionTranslationFilter,-authenticationProcessingFilter,-securityContextPersistenceFilter'], // Stateless chain [pattern: '/**', filters: 'JOINED_FILTERS,-restTokenValidationFilter,-restExceptionTranslationFilter'] // Traditional chain ] 方法:

to_representation