REST URL with tastypie

I am using tastypie in my django application and I am trying to get it to display URLs such as "/ api / booking / 2011/01/01" which maps to the reservation model with the specified timestamp in the URL. The documentation does not allow you to tell how to do this.

+4
source share
1 answer

What do you want to do in your resource, specify

def prepend_urls(self): return [ url(r"^(?P<resource_name>%s)/(?P<year>[\d]{4})/(?P<month>{1,2})/(?<day>[\d]{1,2})%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_list_with_date'), name="api_dispatch_list_with_date"), ] 

which returns a url that points to the view (i called it dispatch_list_with_date), which does what you want.

For example, in the base_urls class, it points to a view called "dispatch_list", which is the main entry point for listing the resource, and you probably just want to sort the replica with your own filtering.

Your opinion may look pretty similar to this.

 def dispatch_list_with_date(self, request, resource_name, year, month, day): # dispatch_list accepts kwargs (model_date_field should be replaced) which # then get passed as filters, eventually, to obj_get_list, it all in this file # https://github.com/toastdriven/django-tastypie/blob/master/tastypie/resources.py return dispatch_list(self, request, resource_name, model_date_field="%s-%s-%s" % year, month, day) 

Indeed, I would just add filter to a regular list resource

 GET /api/booking/?model_date_field=2011-01-01 

You can get this by adding a filter attribute to your Meta class p>

But this is a personal preference.

+11
source

All Articles