我在pyramid_formalchemy中创建自定义表格时遇到问题。我怀疑这个软件包中有一个错误,并且想确认我没有错过任何东西。我的设置是这样的:自定义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,236线起始于get_grid()函数:
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
在这里你可以看到,如果匹配的网格(或字段集)没有发现它会被实例化,但是如果发现类类型将直接使用,但并未实际实例化。
这里有什么想法吗?我是否设置了错误?
基本上我使用CSRF令牌,所以我需要自定义我的窗体以从会话中获取令牌。
谢谢。
这是为什么在stackoverflow?它的表述就像是属于https://github.com/FormAlchemy/pyramid_formalchemy/issues的错误报告 –
我想我应该更好地说出'问题'。我想确认在我发布可能是github的bug之前,我正确地设置了一些东西。 – lostdorje