【问题标题】:Tastypie: How can I fill the resource without database?Tastypie:如何在没有数据库的情况下填充资源?
【发布时间】:2012-10-18 15:54:21
【问题描述】:

我想从 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 个空对象。我应该怎么做才能填补这个资源?

【问题讨论】:

  • 只是不要使用 ModelResource,因为它背后没有模型。
  • 我应该在 Tastypie 上使用什么?
  • 查看我的回复,我已在此处扩展了我的评论。
  • 顺便说一句。此外,您的代码中似乎有 NameError 异常(并非您在函数末尾使用的所有变量都是之前定义的),除非这不是您正在使用的整个代码。

标签: python django foursquare tastypie


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多