Tastypie: How can I fill the resource without database?

2024/5/20 6:30:07

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?

Answer

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

https://en.xdnf.cn/q/72702.html

Related Q&A

Is there a way to protect built-ins in python?

My question arises from this question, in which a user got himself confused by unknowingly rebinding the built-in global set. Is there a straightforward way to get python to warn you when you attempt t…

Generate thumbnail for arbitrary audio file

I want to represent an audio file in an image with a maximum size of 180180 pixels.I want to generate this image so that it somehow gives a representation of the audio file, think of it like SoundCloud…

Extract specific text lines?

I have a large several hudred thousand lines text file. I have to extract 30,000 specific lines that are all in the text file in random spots. This is the program I have to extract one line at a time:b…

Listing users for certain DB with PyMongo

What Im trying to acheiveIm trying to fetch users for a certain database.What I did so farI was able to find function to list the databases or create users but none for listing the users, I thought ab…

Using python selenium for Microsoft edge

I am trying to use pythons selenium for Microsoft edge but I keep getting this error:WebDriverException: Message: unknown error: cannot find Microsoft Edge binaryI downloaded the latest version of the …

get all unicode variations of a latin character

E.g., for the character "a", I want to get a string (list of chars) like "aāăą" (not sure if that example list is complete...) (basically all unicode chars with names "Latin…

How do I install Django on Ubuntu 11.10?

Im using The Definitive guide to installing Django on ubuntu and ironically need something more definitive because I cant make it work.(I have followed the steps before this on the link above) Here is …

Sympy second order ode

I have a homogeneous solution to a simple second-order ODE, which when I try to solve for initial values using Sympy, returns the same solution. It should substitute for y(0) and y(0) and yield a solut…

Bulk update using Peewee library

Im trying to update many records inside a table using Peewee library. Inside a for loop, i fetch a single record and then I update it but this sounds awful in terms of performance so I need to do the u…

Can you specify variance in a Python type annotation?

Can you spot the error in the code below? Mypy cant.from typing import Dict, Anydef add_items(d: Dict[str, Any]) -> None:d[foo] = 5d: Dict[str, str] = {} add_items(d)for key, value in d.items():pr…