使用 tastypie 的 REST url

发布于 2024-11-27 11:38:45 字数 125 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-12-04 11:38:45

您想要在资源中做的是提供一个

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"),
    ]

方法,该方法返回一个 url,该 url 指向一个执行您想要的操作的视图(我将其命名为dispatch_list_with_date)。

例如,在 base_urls 类中,它指向一个名为“dispatch_list”的视图,该视图是列出资源的主要入口点,您可能只想使用自己的过滤来复制该视图。

您的视图可能看起来与此非常相似

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's 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)

,实际上我可能只是添加 filter 到普通列表资源

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

您可以通过向 Meta 类添加过滤属性来获得此属性,

但这是个人偏好。

What you want to do in your Resource is provide an

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"),
    ]

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

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's 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)

Really I would probably just add a filter to the normal list resource

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

You can get this by adding a filtering attribute to your Meta class

But that's a personal preference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文