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

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

本文介绍了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和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 = []

在['场馆']中的场地:
bundle = self.build_bundle(obj =场,请求=请求)
bundle = self.full_dehydrate(bundle)
objects.append(bundle)

返回对象

现在我得到:

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

有2个空对象。为了填补这个资源,该怎么办?

解决方案

ModelResource 只适用于资源背后的ORM模型。在其他情况下,您应该使用资源



此主题在 ModelResource 描述,提及何时适合,何时不适用:



文档中还有一整章,旨在提供有关如何实现非ORM数据源(本例中为外部API)的详细信息:


I want to grab some information from Foursquare , add some fields and return it via django-tastypie.UPDATE:

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

Now I am getting:

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

There are 2 empty objects. What should I do in order to fill this resource?

解决方案

ModelResource is only suitable when you have ORM Model behind the resource. In other cases you should use Resource.

This subject is discussed in ModelResource description, mentioning when it is suitable and when it is not: http://django-tastypie.readthedocs.org/en/latest/resources.html#why-resource-vs-modelresource

Also there is a whole chapter in the documentation, aimed at providing the details on how to implement non-ORM data sources (in this case: external API): http://django-tastypie.readthedocs.org/en/latest/non_orm_data_sources.html

这篇关于Tastypie:如何在没有数据库的情况下填充资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:43