6

我想从 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

现在我得到:

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

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

4

1 回答 1

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

于 2012-10-18T16:31:08.553 回答