gpt4 book ai didi

python - Pyramid "model"也是 Pyramid "resource"吗?

转载 作者:行者123 更新时间:2023-12-02 06:45:58 27 4
gpt4 key购买 nike

我目前正在学习如何使用 Python Pyramid Web 框架,并且发现文档非常出色。

然而,在区分“模型”(即在 SQLAlchemy 声明性系统下定义的类)的概念和“资源”(即定义访问控制的方法)的概念时,我遇到了一个绊脚石列出与 Pyramid 的身份验证系统一起使用的 View )。

我理解上述陈述似乎表明我已经理解了差异,但我无法理解是否应该创建模型资源(通过直接在模型类中添加 __acl__ 属性)或创建一个单独的资源类(具有适当的 __parent____name__ 属性)来表示对使用该模型的 View 的访问。

感谢任何指导。

最佳答案

如果应用程序的域模型是分层的,Pyramid 提供了资源的思想来构建资源树。遍历用于将 URL 映射到代码并识别资源树中的资源。使用关系数据库时,您通常不使用资源和遍历。

摘自"Defending the design - Pyramid Does Traversal, and I Don't Like Traversal"

In Pyramid, traversal is the act of resolving a URL path to a resource object in a resource tree. Some people are uncomfortable with this notion, and believe it is wrong. Thankfully if you use Pyramid and you don't want to model your application in terms of a resource tree, you needn't use it at all. Instead use URL dispatch to map URL paths to views.

Relational databases aren't naturally hierarchical, so traversing one like a tree is not possible.

You can be assured that if you don't want to understand traversal, you don't have to. You can happily build Pyramid applications with only URL dispatch.

摘自Resources

A resource is an object that represents a "place" in a tree related toyour application. (...) A resource tree is a set of nesteddictionary-like objects which you can use to represent your website'sstructure.

In an application which uses traversal to map URLs to code, theresource tree structure is used heavily to map each URL to a viewcallable. When traversal is used, Pyramid will walk through theresource tree by traversing through its nested dictionary structure inorder to find a context resource. Once a context resource is found,the context resource and data in the request will be used to find aview callable.

In an application which uses URL dispatch, the resource tree is onlyused indirectly, and is often "invisible" to the developer. (...) This root resource sometimes has securitydeclarations attached to it, but is not required to have any. Ingeneral, the resource tree is much less important in applications thatuse URL dispatch than applications that use traversal.

我认为文档中广泛涵盖了该主题。

我曾经推荐过一个项目来强调 Pyramid 的功能。

我的拙见:您不需要预先完全理解这两个概念,就可以在您的第一个项目中采用 Pyramid 框架。使用关系数据库时,请使用 URL Dispatch 和 SQLAlchemy。

Excerpt - Pyramid Provides Too Many "Rails"

By design, Pyramid is not a particularly opinionated web framework. Pyramid provides some features that other web frameworks do not. These are features meant for use cases that might not make sense to you if you're building a simple (...) web application.

关于python - Pyramid "model"也是 Pyramid "resource"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54869002/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com