2011-10-29 44 views
2

我在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令牌,所以我需要自定義我的窗體以從會話中獲取令牌。

謝謝。

+0

這是爲什麼在stackoverflow?它的表述就像是屬於https://github.com/FormAlchemy/pyramid_formalchemy/issues的錯誤報告 –

+1

我想我應該更好地說出'問題'。我想確認在我發佈可能是github的bug之前,我正確地設置了一些東西。 – lostdorje

回答

5

pyramid_formalchemy會對你如何設置你的表單文件做出一定的,主要是沒有記錄的假設。下面是我打了周圍工作的陷阱......

  1. 如果你有例如模型的用戶,那麼你就需要有一個字段名爲UserFieldSet。

  2. UserFieldSet必須是實例而不是類。

  3. 要小心你的進口,否則你會打破pyramid_formalchemy的假設。如果您有模型類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 group獲取更多信息。