是否可以设置 django-tastypie 对象键?

发布于 2025-01-07 20:24:56 字数 646 浏览 0 评论 0原文

默认情况下,当使用 django-tastypie 并获取资源列表时,响应的格式

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 3
    },
    "objects": [{
        "body": "Welcome to my blog!",
        "id": "1",
        "pub_date": "2011-05-20T00:46:38",
        "resource_uri": "/api/v1/entry/1/",
        "slug": "first-post",
        "title": "First Post",
        "user": "/api/v1/user/1/"
    },
    ...
    ]
}

如下 :看起来,但我似乎找不到任何类型的元选项或设置来更改“对象”键以实际描述返回的项目。例如,假设我在一个 api 调用中有位置列表,在另一个 api 调用中有人员列表。我希望能够区分“地点”和“人”的关键。真正的原因是因为我在 iOS 上使用 RestKit 并且希望能够设置多个映射。

By default, when using django-tastypie and fetching a resource list, the response is of the format:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 3
    },
    "objects": [{
        "body": "Welcome to my blog!",
        "id": "1",
        "pub_date": "2011-05-20T00:46:38",
        "resource_uri": "/api/v1/entry/1/",
        "slug": "first-post",
        "title": "First Post",
        "user": "/api/v1/user/1/"
    },
    ...
    ]
}

I've dug into the documentation and looked & looked, but I can't seem to find any kind of meta option or setting to change the "objects" key to actually describe the returned items. For example, let's say I have list of locations in one api call and a list of people in another. I'd like to be able to differentiate the key to "locations" and "people". The real reason for this is because I'm using RestKit on iOS and want to be able to set up multiple mappings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作死小能手 2025-01-14 20:24:56

资源挂钩 alter_* 可用于更改数据的结构。

使用“位置”的资源示例如下:

class MyLocationsResource(ModelResource):
    def alter_list_data_to_serialize(self, request, data):
        data['locations'] = data['objects']
        del data['objects']
        return data

    def alter_deserialized_list_data(self, request, data):
        data['objects'] = data['locations']
        del data['locations']
        return data

The Resource hooks alter_* can be used to alter the structure of the data.

An example Resource using 'locations' would be:

class MyLocationsResource(ModelResource):
    def alter_list_data_to_serialize(self, request, data):
        data['locations'] = data['objects']
        del data['objects']
        return data

    def alter_deserialized_list_data(self, request, data):
        data['objects'] = data['locations']
        del data['locations']
        return data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文