Tastypie: How can I fill the resource without database?
Asked Answered
J

1

6

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?

Joanniejoao answered 18/10, 2012 at 15:54 Comment(4)
Just do not use ModelResource, as you do nit have model behind it.Supinator
What should I use with Tastypie?Joanniejoao
See my response, I have expanded my comment there.Supinator
Btw. Also it looks like you have NameError exception in your code (not all the variables you use at the end of the function are defined before), unless this is not the whole code you are using.Supinator
S
9

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

Supinator answered 18/10, 2012 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.