Tastypie:如何在没有数据库的情况下填充资源?

Bur*_*rak 6 python django foursquare tastypie

我想从Foursquare获取一些信息,添加一些字段并通过django-tastypie返回.更新:

def obj_get_list(self, request=None, **kwargs):
    near = ''
    if 'near' in request.GET and request.GET['near']:
        near = request.GET['near']
    if 'q' in request.GET and request.GET['q']:
        q = request.GET['q']

    client = foursquare.Foursquare(client_id=settings.FSQ_CLIENT_ID, client_secret=settings.FSQ_CLIENT_SECRET)

    a = client.venues.search(params={'query': q, 'near' : near, 'categoryId' : '4d4b7105d754a06374d81259' })

    objects = []

    for venue in a['venues']:
        bundle = self.build_bundle(obj=venue, request=request)
        bundle = self.full_dehydrate(bundle)
        objects.append(bundle)

    return objects
Run Code Online (Sandbox Code Playgroud)

现在我得到:

{
  "meta": {
    "limit": 20,
    "next": "/api/v1/venue/?q=Borek&near=Kadikoy",
    "offset": 0,
    "previous": null,
    "total_count": 30
  },
  "objects": [
    {
      "resource_uri": ""
    },
    {
      "resource_uri": ""
    }]
}
Run Code Online (Sandbox Code Playgroud)

有2个空对象.我该怎么做才能填补这个资源?

Tad*_*eck 9

ModelResource仅适用于资源后面有ORM模型的情况.在其他情况下你应该使用Resource.

这个主题在ModelResource描述中进行了讨论,提到它何时适用,何时不适用:http://django-tastypie.readthedocs.org/en/latest/resources.html#why-resource-vs-modelresource

此外,文档中还有一章,旨在提供有关如何实现非ORM数据源的详细信息(在本例中为:外部API):http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources html的