Hello all,

Been using web2py for about a week and loving it so far.  In particular the 
built in access control is great coming from Flask.

I'm having some issues referencing auth_user from other db fields though.  
In the following view (company.html) I have a SQLFORM for the 'company' 
table.  How do I set the user_id field of the company form to default to 
whatever the current logged in user's ID is?  I tried adding a field called 
user_id to the company table, but for some reason it causes the form to 
invalidate every time. 

I have the following code within the models folder in another file called 
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')

And an my regular db.py file:

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'),
]

The controller for company.  When I navigate to this view the User field 
shows up in the data grid, but not in the forms.  (And I don't want it to, 
I just want it to be set to whatever user is logged in, i.e. auth.user).  
For whatever reason the form won't validate.  I think it has something to 
do with the model.

@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()

Here's the view for company:

{{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}}

Thanks!
--Hunt



 

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to