【发布时间】:2014-04-17 21:58:53
【问题描述】:
我有一个复杂的 Django 模型,其中包含 (1) 很多字段和 (2) 为该模型动态生成的未存储在模型中的数据。
复杂资源:
class ComplexResource(resource):
class Meta:
allowed_methods = ('get','put','post','delete','patch')
queryset = Complex.objects.all()
serializer = CustomSerializer()
authorization = Authorization()
authentication = Authentication()
always_return_data = True
filtering = { "field_1" : ALL, "field_2" : ALL, "field_N" : ALL }
ordering = ["field_1", "field_2", "field_n"]
目前,我的复杂端点/m/api/v1/complex/ 成功返回模型上的存储数据。
复杂的反应:
{ "meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 1
},
"objects": [
{"field_1": "foo", "field_2": "bar", ... , "field_n": "foobar"}
]
}
问题:
我想做的是使用prepend_urls 添加一个自定义端点,它将返回所有数据。这样我就不必每次调用当前端点时都使用 dehydrate 浪费性地添加数据,可以在这里看到:
http://django-tastypie.readthedocs.org/en/latest/cookbook.html#adding-custom-values
问题
如何创建一个prepend_url 将附加数据注入资源?还是有更好的设计模式可以完全解决这个问题?
包含所有数据的复杂响应:/m/api/v1/complex/all/
{ "meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 1
},
"objects": [
{"field_1": "foo",
"field_2": "bar",
.
.
.
"field_n": "foobar",
"advanced_field_1 : "a1",
"advanced_field_2 : "a2",
.
.
.
"advanced_field_n : "an",
}
]
}
【问题讨论】:
标签: django rest tastypie denormalization