Tastypie:我怎样才能弥补资源没有数据库?(Tastypie: How can I fill

2019-08-01 14:44发布

我想抓住从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个空的对象。 我应该为了填补这一资源呢?

Answer 1:

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的



文章来源: Tastypie: How can I fill the resource without database?