自定义 Pyramid_Formalchemy 形式

发布于 2024-12-12 12:57:05 字数 2729 浏览 0 评论 0原文

我在 Pyramid_formalchemy 中创建自定义表单时遇到问题。我怀疑包裹中存在错误,并想确认我没有丢失任何东西。我的设置如下所示:

def includeme(config):
    config.include('pyramid_formalchemy')
    # Adding the jquery libraries
    config.include('fa.jquery')
    # Adding the package specific routes
    config.include('myapp.web.formalchemy.faroutes')

    config.formalchemy_admin('admin',
                             models=[User],
                             forms=faforms,
                             session_factory=session,
                             view='fa.jquery.pyramid.ModelView',
                             factory='myapp.model.RootFactory')

    config.formalchemy_model('user',
                             model='myapp.model.user.User',
                             session_factory=session,
                             view='fa.jquery.pyramid.ModelView',
                             factory='myapp.model.RootFactory')

faforms 是一个包含我的自定义表单的模块:

from myapp.model.user import User

from formalchemy import FieldSet
from formalchemy import Grid

class UserFieldSet(FieldSet):
    def __init__(self):
        FieldSet.__init__(self, User)
        self.configure()

class UserGrid(Grid):
    def __init__(self):
        Grid.__init__(self, User)
        self.configure()

如果我注释掉上面的两个类,formalchemy 就可以正常工作。我可以查看用户并可以编辑它们。

当我把这两门课放进去时,我遇到了问题。问题是pyramid_formalchemy从模块的命名空间中获取UserGrid和UserFieldSet,然后尝试像实例化类一样使用它们。这会破坏事情。另一方面,如果 Pyramid_formalchemy 找不到类,将动态创建类并实例化它们。我相信有问题的代码位于 Pyramid_formalchemy/views.py 中,从 get_grid() 函数开始的第 236 行:

def get_grid(self):
    """return a Grid object"""
    request = self.request
    model_name = request.model_name
    form_name = '%sGrid' % model_name
    if hasattr(request.forms, form_name):
        g = getattr(request.forms, form_name)  <-- g is a class type not an
        g.engine = g.engine or self.engine     <-- instance!
        g.readonly = True                      <-- why is it not instantiated?
        g._request = self.request
        self.update_grid(g)
        return g
    model = self.context.get_model()           <-- UserGrid not found in faforms 
    grid = self.grid_class(model)              <-- module.
    grid.engine = self.engine                  <-- so a Grid is instantiated
    if not isinstance(request.forms, list):
        # add default grid to form module eg: caching
        setattr(request.forms, form_name, grid)
    grid = grid.copy()
    grid._request = self.request
    self.update_grid(grid)
    return grid

在这里您可以看到是否找不到匹配的网格(或字段集),它将被实例化,但如果找到了该类type 将被直接使用,但不会实际实例化。

这里有什么想法吗?我是否设置错误?

基本上我使用的是 CSRF 令牌,因此我需要自定义表单以从会话中获取令牌。

谢谢。

I'm having problems creating custom forms in pyramid_formalchemy. I suspect there is a bug in the package and wanted to confirm I'm not missing anything. My setup looks like this:

def includeme(config):
    config.include('pyramid_formalchemy')
    # Adding the jquery libraries
    config.include('fa.jquery')
    # Adding the package specific routes
    config.include('myapp.web.formalchemy.faroutes')

    config.formalchemy_admin('admin',
                             models=[User],
                             forms=faforms,
                             session_factory=session,
                             view='fa.jquery.pyramid.ModelView',
                             factory='myapp.model.RootFactory')

    config.formalchemy_model('user',
                             model='myapp.model.user.User',
                             session_factory=session,
                             view='fa.jquery.pyramid.ModelView',
                             factory='myapp.model.RootFactory')

faforms is a module containing my custom forms:

from myapp.model.user import User

from formalchemy import FieldSet
from formalchemy import Grid

class UserFieldSet(FieldSet):
    def __init__(self):
        FieldSet.__init__(self, User)
        self.configure()

class UserGrid(Grid):
    def __init__(self):
        Grid.__init__(self, User)
        self.configure()

If I comment out the two classes above, formalchemy works fine. I can view Users and I can edit them.

When I put the two classes in I run into problems. The problem is pyramid_formalchemy grabs UserGrid and UserFieldSet from the module's namespace and then tries to use them as if they were instantiated classes. This breaks things. On the other hand if pyramid_formalchemy doesn't find the classes in will dynamically create the classes AND instantiate them. I believe the offending code is in pyramid_formalchemy/views.py, line 236 starting at the get_grid() function:

def get_grid(self):
    """return a Grid object"""
    request = self.request
    model_name = request.model_name
    form_name = '%sGrid' % model_name
    if hasattr(request.forms, form_name):
        g = getattr(request.forms, form_name)  <-- g is a class type not an
        g.engine = g.engine or self.engine     <-- instance!
        g.readonly = True                      <-- why is it not instantiated?
        g._request = self.request
        self.update_grid(g)
        return g
    model = self.context.get_model()           <-- UserGrid not found in faforms 
    grid = self.grid_class(model)              <-- module.
    grid.engine = self.engine                  <-- so a Grid is instantiated
    if not isinstance(request.forms, list):
        # add default grid to form module eg: caching
        setattr(request.forms, form_name, grid)
    grid = grid.copy()
    grid._request = self.request
    self.update_grid(grid)
    return grid

Here you can see if the matching grid (or fieldset) is not found it will be instantiated, but if it is found the class type will be used directly, but not actually instantiated.

Any thoughts here? Am I setting something up wrong?

Basically I'm using CSRF tokens so I need to customize my forms to grab the tokens from the session.

Thanks.

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

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

发布评论

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

评论(1

美人迟暮 2024-12-19 12:57:05

Pyramid_formalchemy 对如何设置表单文件做出了某些大多数未记录的假设。以下是我遇到并解决的问题...

  1. 例如,如果您有一个 User 模型,那么您将需要一个名为 UserFieldSet 的 FieldSet。

  2. UserFieldSet 必须是实例,而不是类。

  3. 小心你的导入,否则你会打破pyramid_formalchemy所做的假设。如果您有模型类 User,请导入 User 的包,但不要导入 User 类本身。然后通过在引用前加上包名来引用该类。

下面是一个使用上述 3 点的工作示例。

from myapp.model import user

from formalchemy import Field
from formalchemy import FieldSet
from formalchemy import Grid

UserFieldSet = FieldSet(user.User)
UserFieldSet.configure()

UserGrid = Grid(user.User)
UserGrid.configure()

您还可以在formalchemy 群组发帖了解更多信息。

pyramid_formalchemy makes certain, mostly undocumented assumptions, about how you setup your forms file. Here are the gotchas I hit and worked around...

  1. If you have a model User for example, then you will need to have a FieldSet called UserFieldSet.

  2. UserFieldSet must be an instance and not a class.

  3. Be careful on your imports or you will break assumptions pyramid_formalchemy is making. If you have a model class User, import User's package, but not the User class itself. Then reference the class by prefixing the reference with the package name.

Below is a working example using the 3 points above.

from myapp.model import user

from formalchemy import Field
from formalchemy import FieldSet
from formalchemy import Grid

UserFieldSet = FieldSet(user.User)
UserFieldSet.configure()

UserGrid = Grid(user.User)
UserGrid.configure()

You can also post to the formalchemy group for more info.

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