有没有办法让美味饼中的脱水功能共享一些变量?在任何其他框架中,每个请求都将创建该类的新实例,因此我们可以使用prefetch_related
共享数据。
我的用例:想用一些其他数据使GET列表返回的对象脱水,但是由于重复的db调用,请求变得很重。并非所有内容都来自标准sql数据库,所以class Meta:
queryset = Entry.objects.all()
list_allowed_methods = ['get', 'post']
detail_allowed_methods = ['get', 'post', 'put', 'delete']
def dehydrate(self, bundle):
# # wanted something like this
# bundle.foo = self.cache['foo']
# # but self is shared between all requests as an instance
# # of this class is declared while initializing, and I want
# # self.cache to be recreated for every request (without
# # potential races)
urlpatterns = [
url(r'^api/', include(EntryResource().urls)),
]
只会带我走那么远。
m_type = TypeA;
答案 0 :(得分:0)
最后,我使用bundle.request
保存数据。
基本上:
try:
bundle.request.cache
except AttributeError:
bundle.request.cache = {}