python – 金字塔“模型”也是金字塔“资源”吗?

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

然而,当涉及将“模型”(即在sqlAlchemy的声明系统下定义的类)与“资源”的概念(即在视图上定义访问控制列表的方法)区分开来时,我遇到了绊脚石.用于Pyramid的auth系统).

我理解上面的陈述似乎表明我已经理解了差异,但我无法理解我是否应该创建模型资源(通过直接在模型类中添加__acl__属性)或创建一个单独的资源类(具有正确的__parent__和__name__属性),表示对使用该模型的视图的访问.

任何指导表示赞赏.

最佳答案
如果应用程序的域模型是分层次的,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 to
your application. (…) A resource tree is a set of nested
dictionary-like objects which you can use to represent your website’s
structure.

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

In an application which uses URL dispatch,the resource tree is only
used indirectly,and is often “invisible” to the developer. (…) This root resource sometimes has security
declarations attached to it,but is not required to have any. In
general,the resource tree is much less important in applications that
use URL dispatch than applications that use traversal.

我认为这个主题在文档中有很多内容.

> Resources
> Much ado about traversal
> Traversal
> Combining Traversal and URL Dispatch

我过去常常推荐一个强调Pyramid功能的项目.

> Pyramid Auth Demo

我的拙见:您不需要完全理解这两个概念,也不需要为您的第一个项目采用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.

相关文章

在这篇文章中,我们深入学习了XPath作为一种常见的网络爬虫技巧。XPath是一种用于定位和选择XML文档中特...
祝福大家龙年快乐!愿你们的生活像龙一样充满力量和勇气,愿你们在新的一年里,追逐梦想,勇往直前,不...
今天在爬虫实战中,除了正常爬取网页数据外,我们还添加了一个下载功能,主要任务是爬取小说并将其下载...
完美收官,本文是爬虫实战的最后一章了,所以尽管本文着重呈现爬虫实战,但其中有一大部分内容专注于数...
JSON是一种流行的数据传输格式,Python中有多种处理JSON的方式。官方的json库是最常用的,它提供了简单...
独立样本T检验适用于比较两组独立样本的均值差异,而配对T检验则适用于比较同一组样本在不同条件下的均...