2013-08-05 136 views
0

我遇到了一些引用來自其他數據庫字段的auth_user的問題。在下面的視圖(company.html)中,我有一個針對'公司'表的SQLFORM。如何將公司表單的user_id字段設置爲默認值,無論當前登錄用戶的ID是什麼?我試圖在公司表中添加一個名爲user_id的字段,但由於某種原因,它會導致表單每次都失效。如何將auth.user.id設置爲SQLFORM中字段的默認值

我在叫db_tasks.py另一個文件夾模式中的以下代碼:

# Define company table 
db.define_table('company', 
Field('company_name', notnull=True, unique=True), 
Field('user_id', db.auth_user, default=auth.user_id), 
Field('email'), 
Field('phone', notnull=True), 
Field('url'), 
format = '%(company_name)s') 

而一個我的正常db.py文件:

db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all']) 
auth = Auth(db) 

# Add extra user fields 
auth.settings.extra_fields['auth_user'] = [ 
    Field('address'), 
    Field('city'), 
    Field('zip'), 
    Field('image', 'upload'), 
] 

控制器的公司。當我導航到此視圖時,用戶字段顯示在數據網格中,但不顯示在表單中。 (我不希望它,我只是希望它被設置爲任何用戶登錄,即auth.user)。無論出於何種原因,表單都不會進行驗證。我認爲這與模型有關。

@auth.requires_login() 
def company(): 
# init a form object for 'company' table 
company_form = SQLFORM(db.company).process() 
# init a grid object for 'company' table 
grid = SQLFORM.grid(db.company, create=False, deletable=False, editable=False, maxtextlength=50, orderby=db.company.company_name) 
return locals() 

下面是公司的觀點:

{{extend 'layout.html'}} 

<h2>Add a new company:</h2> 
<br/> 

{{=company_form.custom.begin}} 
<strong>Company Name</strong>   <br/>{{=company_form.custom.widget.company_name}}<br/> 
<strong>Email</strong><br/>{{=company_form.custom.widget.email}}<br/> 
<strong>Phone</strong><br/>{{=company_form.custom.widget.phone}}<br/> 
<strong>URL</strong><br/>{{=company_form.custom.widget.url}}<br/> 
{{=company_form.custom.submit}} 
{{=company_form.custom.end}} 

<br/> 
<br/> 
<h2>All Companies:</h2> 
<br/> 
{{=grid}} 

回答

0

什麼是 「session.auth.user.id if session.auth else None」?

+0

想通了。出於某種原因,db_taks.py中的可寫字段必須爲False。不過謝謝 –

相關問題