Django Tastypie覆盖网址与slug [英] Django Tastypie Override URL with slug

查看:169
本文介绍了Django Tastypie覆盖网址与slug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的地址:

  def override_urls(self):
return [
url (r^(?P< resource_name>%s)/(?P< slug> [\w\d _.-] +)/ $%self._meta.resource_name,self.wrap_view('dispatch_detail' ,name =api_dispatch_detail),
]

p>

  / api / v1 / nodes /< slug> / 
self.get_resource_uri(bundle)返回 / api / v1 / nodes / p

< id> / ,我无法有效地比较当前的URL与资源URI。



我做错了什么?

解决方案:工作代码



我在这里实现了提出的解决方案:
https://github.com/ninuxorg/nodeshot/blob/refactoring/nodeshot/core/base/ resources.py



解决方案

您可以覆盖 get_resource_uri 在您的资源上返回正确的uri。毕竟,正确的是一个lug the,因为那是你的资源捕获的(第一个)。



更新 p>

正确的方法是跳过 override_urls 并将其放在资源的元数据上:

  detail_uri_name ='slug'

TLDR背景



我挖了一点,看起来好像有一个更好的地方来实现。调用 get_resource_uri (间接)的默认实现调用 self.detail_uri_kwargs 然后 reverse



ModelResource 中的 detail_uri_kwargs 的默认实现只是看起来up self._meta.detail_uri_name (其名称不直观),并抓住模型的关键。 detail_uri_name 默认为 pk



如果你只是在这里提供一个不同的名字,你可以跳过override_urls和get_resource_uri!



这个(建立在由OP的意见链接的代码上):

  from tastypie.resources import ModelResource 
from tastypie .bundle import Bundle

class BaseSlugResource(ModelResource):
基本模型资源使用slug urls

class Meta:
abstract = True
detail_uri_name ='slug'

我不知道我的顶部头是否资源 Meta s是继承的(我要猜测他们不是),所以这可能无法作为基类工作。幸运的是,需要的一行可能会粘贴到需要它的每个资源中。


I have a similar coce:

def override_urls(self):
    return [
        url(r"^(?P<resource_name>%s)/(?P<slug>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
    ]

Which produces an URL like:

/api/v1/nodes/<slug>/

Everything fine except that self.get_resource_uri(bundle) returns /api/v1/nodes/<id>/ and I cannot compare the current URL with the resource URI effectively.

What am I doing wrong?

Solution: working code

I implemented the proposed solution here: https://github.com/ninuxorg/nodeshot/blob/refactoring/nodeshot/core/base/resources.py

Any additional feedback for improvement is welcome.

解决方案

You could override get_resource_uri on your resource to return the correct uri. After all, the correct one is the one with the slug since that is the (first) one captured by your resource.

Update

The right way to do this is actually to skip override_urls and put this on the Resource's Meta:

detail_uri_name = 'slug'

TLDR Background

I dug a bit deeper and it looks like there's a much better place to implement this. The default implementation of get_resource_uri (indirectly) calls self.detail_uri_kwargs and then reverse.

The default implementation of detail_uri_kwargs in ModelResource just looks up self._meta.detail_uri_name (whose name is not intuitive), and grabs that key off the model. detail_uri_name defaults to pk.

If you just provide a different name here, you can skip the override_urls and the get_resource_uri!

Something like this (building on the code linked in comments by the OP):

from tastypie.resources import ModelResource
from tastypie.bundle import Bundle

class BaseSlugResource(ModelResource):
    """ Base Model Resource using slug urls """

    class Meta:
        abstract = True
        detail_uri_name = 'slug'

I'm not sure off the top of my head whether resource Metas are inherited (I'm going to guess they're not), so this may not work as a base class. Luckily, the one line required is probably fine to paste into each Resource that needs it.

这篇关于Django Tastypie覆盖网址与slug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆