gpt4 book ai didi

django - 获取 tastypie 中的对象列表(在另一个 View 中)

转载 作者:行者123 更新时间:2023-12-02 02:08:01 27 4
gpt4 key购买 nike

我正在尝试获取 Tastypie 响应以在另一个 View 中使用。我见过the recipe in the cookbook 。问题是,我想获得 ListView 。就我而言,/api/v1/source/。这是我到目前为止所得到的:

sr = SourceResource()
objs = sr.get_object_list(request) # two objects returned
bun = sr.build_bundle(data=objs, request=request)

jsondata = sr.serialize(None, sr.full_dehydrate(bun), 'application/json')

当然,这一切都会崩溃。 bun.data 不具备所需的特征(单个对象)。那么,有人成功做到这一点吗?它是如何完成的?

最佳答案

这就是我的想法。我不太喜欢请求和 QueryDict 都被复制,但除了复制 the tastypie code 的大部分内容之外,我目前想不出其他任何东西。 .

from copy import copy

from django.views.generic import TemplateView

from incremental.sources.resources import SourceResource
resource = SourceResource()

class AppView(TemplateView):
'Base view for the Source parts of the app'
template_name = 'sources/base.html'

def get_context_data(self, **data):
'get context data'
tmp_r = copy(self.request)
tmp_r.GET = tmp_r.GET.copy()
tmp_r.GET['format'] = 'json'

data.update({
'seed': resource.get_list(tmp_r).content
})
return data

关于django - 获取 tastypie 中的对象列表(在另一个 View 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10843232/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com