是否可以在 Django 项目中重新定义反向?

发布于 2024-08-10 00:18:28 字数 641 浏览 3 评论 0原文

我有一些自定义逻辑,每次 URL 反转时都需要执行,即使对于第三方应用程序也是如此。我的项目是一个多租户Web应用程序,租户是根据URL来识别的。没有一个有效的 URL 不包含租户标识符。

我已经有一个围绕 reverse 的包装函数,但现在我需要一种方法来告诉每个已安装的应用程序使用它。 reverse 周围的包装器使用线程本地将标识符注入到结果 URL 中。我可以将此函数编写为 reverse 上的装饰器,但我不知道在哪里进行实际装饰。

适度严格的约束:我已经使用了 3 个第 3 方应用程序,并且我可能会添加更多。解决方案不应该要求我修改所有这些第三方应用程序的源代码。如果有更简单的方法的话,我不喜欢在多个第 3 方源代码树之上维护补丁的想法。我可以让文档非常清楚地表明 reverse 已被修饰。

最初的问题:我可以在哪里进行这样的更改以保证它适用于 reverse 的每次调用?

可能的替代问题:确保每个 URL(包括由第 3 方应用程序生成的 URL)获取租户标识符的更好方法是什么? 顺便说一句,我愿意接受更好的方法来处理这些问题,除了在 URL 中嵌入租户 ID 之外;这个决定现在已经是板上钉钉的事了。谢谢。

谢谢。

I have some custom logic that needs to be executed every single time a URL is reversed, even for third-party apps. My project is a multitenant web app, and the tenant is identified based on the URL. There isn't a single valid URL that doesn't include a tenant identifier.

I already have a wrapper function around reverse, but now I need a way to tell every installed app to use it. The wrapper around reverse uses a thread-local to inject the identifier into the resulting URL. I could write this function as a decorator on reverse, but I don't know where to do the actual decoration.

Moderately Firm Constraint: I'm already using 3 3rd-party apps, and I'll probably add more. A solution should not require me to modify the source code of all these third-party apps. I don't relish the idea of maintaining patches on top of multiple 3rd-party source trees if there is an easier way. I can make the documentation abundantly clear that reverse has been decorated.

The Original Question: Where could I make such a change that guarantees it would apply to every invocation of reverse?

Possible Alternate Question: What's a better way of making sure that every URL—including those generated by 3rd-party apps—gets the tenant identifier?
BTW, I'm open to a better way to handle any of this except the embedding of the tenant-id in the URL; that decision is pretty set in stone right now. Thanks.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

只是我以为 2024-08-17 00:18:28

django reverse 被 ur_reverse 取代的唯一方法是

django.core.urlresolvers.reverse = ur_reverse

或者如果你喜欢装饰器语法糖

django.core.urlresolvers.reverse = ur_reverse_decorator(django.core.urlresolvers.reverse )

,我不会建议(很多人会大喊),除非你不愿意用 ur_reverse 改变 reverse 的每一个用法

only way so that django reverse is replaced by ur_reverse is

django.core.urlresolvers.reverse = ur_reverse

or if you like decorator syntactic sugar

django.core.urlresolvers.reverse = ur_reverse_decorator(django.core.urlresolvers.reverse )

which i would not advice(and many will shout), unless you are not willing to change every usage of reverse with ur_reverse

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