使用 tastypie 的 REST url
我在 django 应用程序中使用 tastypie,并尝试让它映射像“/api/booking/2011/01/01”这样的 URL,该 URL 映射到 URL 中具有指定时间戳的 Booking 模型。该文档没有说明如何实现这一点。
I'm using tastypie in my django application and I'm trying to get it to map urls like "/api/booking/2011/01/01" which maps to a Booking model with the specified timestamp in the url. The documentation falls short of telling how to achieve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要在资源中做的是提供一个
方法,该方法返回一个 url,该 url 指向一个执行您想要的操作的视图(我将其命名为dispatch_list_with_date)。
例如,在 base_urls 类中,它指向一个名为“dispatch_list”的视图,该视图是列出资源的主要入口点,您可能只想使用自己的过滤来复制该视图。
您的视图可能看起来与此非常相似
,实际上我可能只是添加 filter 到普通列表资源
您可以通过向 Meta 类添加过滤属性来获得此属性,
但这是个人偏好。
What you want to do in your Resource is provide an
method, which returns a url, which points to a view (I named it dispatch_list_with_date) that does what you want.
For example, in the base_urls class, it points to a view called 'dispatch_list' that's the primary entry point for listing a resource, and you'll probably just want to sort of replicate that with your own filtering.
Your view might look pretty similar to this
Really I would probably just add a filter to the normal list resource
You can get this by adding a filtering attribute to your Meta class
But that's a personal preference.