Django-tastypie creates a hierarchy of URLs

I would like to create a hierarchy of URLs using Tastypie, but with some errors. This is how I want the hierarchy to work:

/recipe /recipe/ID /recipe/ID/spice /recipe/ID/spice/ID 

I can’t find out how to do this. When I installed this according to the Tastypi instructions, my URLs would be like this:

 /recipe /recipe/ID /spice /spice/ID 

If I change the resource name for spice to "/ recipe / spice", I get the message " NotFound: Invalid resource lookup data (inappropriate type) .

Any suggestions on what I can do?

+4
source share
1 answer

Tastypie is designed to help implement the REST API and, thus, by default only supports URLs that conform to REST methods. Namely, each URL must contain the name of the resource (“recipe” or “spice”) and optionally an identifier for this resource (“ID”). Anything outside of this violates the REST rules, and if you do not implement the REST API, you may need to reconsider whether you should use Tastypie.

In this way, Tastypie provides a ton of hooks for customizing things. For custom URLs, you want to define an override_urls method to map specific URLs to custom views and do preprocessing before sending them to regular dispatchers.

If possible, I would recommend simply using standard REST methods and breaking things up as separate recipe and spice resources. If you need to filter recipes based on the spices that are in them, the “spices” should be passed as a GET parameter, not part of the base URL. Hope this helps.

+4
source

Source: https://habr.com/ru/post/1399681/


All Articles