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

前端之家收集整理的这篇文章主要介绍了python – Tastypie:如何在没有数据库的情况下填充资源?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想从Foursquare获取一些信息,添加一些字段并通过 django-tastypie返回.
更新:
  1. def obj_get_list(self,request=None,**kwargs):
  2. near = ''
  3. if 'near' in request.GET and request.GET['near']:
  4. near = request.GET['near']
  5. if 'q' in request.GET and request.GET['q']:
  6. q = request.GET['q']
  7.  
  8. client = foursquare.Foursquare(client_id=settings.FSQ_CLIENT_ID,client_secret=settings.FSQ_CLIENT_SECRET)
  9.  
  10. a = client.venues.search(params={'query': q,'near' : near,'categoryId' : '4d4b7105d754a06374d81259' })
  11.  
  12. objects = []
  13.  
  14. for venue in a['venues']:
  15. bundle = self.build_bundle(obj=venue,request=request)
  16. bundle = self.full_dehydrate(bundle)
  17. objects.append(bundle)
  18.  
  19. return objects

现在我得到:

  1. {
  2. "Meta": {
  3. "limit": 20,"next": "/api/v1/venue/?q=Borek&near=Kadikoy","offset": 0,"prevIoUs": null,"total_count": 30
  4. },"objects": [
  5. {
  6. "resource_uri": ""
  7. },{
  8. "resource_uri": ""
  9. }]
  10. }

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

解决方法

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

这个主题在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

猜你在找的Python相关文章