Hi.
I'm new by here, as also in web2py. I'm from Brazil.
I created a table with this field:
Field('userid', db.auth_user, unique=True, required=True,
writable=False, readable=False)
Just logged users can submit, with 1 record per user, and I need to keep
the user id in each record.
In the controller I put this in the CRUD creation:
# coding: utf8#
from gluon.tools import Crud
crud = Crud(globals(), db)
crud.settings.formstyle = "divs"
crud.messages.submit_button = 'Enviar'
@auth.requires_login()
def index():
uid = auth.user_id
current_records = db(db.guys.userid == uid)
if current_records.count(): #user already registry
gid = current_records.select().first().id
f = crud.update(db.guys, gid, audit_guys,
onaccept=crud.archive) #To be fixed
else: # New regisry
db.guys.pg.readable = False
db.guys.nome.default = auth.user.first_name+ ' ' + auth.user.last_name
db.guys.email.default = auth.user.email
#db.guys.userid.default = auth.user_id
db.guys.userid.compute = lambda f: uid
f = crud.create(db.guys)
return dict(msg='',form=f)
But when I insert a record, it's raises this error:
Traceback (most recent call last):
File "/var/www/web2py/gluon/restricted.py", line 188, in restricted
exec ccode in environment
File "/var/www/web2py/applications/jornada/controllers/inscricao.py",
line 59, in <module>
File "/var/www/web2py/gluon/globals.py", line 124, in <lambda>
self._caller = lambda f: f()
File "/var/www/web2py/gluon/tools.py", line 2331, in f
return action(*a, **b)
File "/var/www/web2py/applications/jornada/controllers/inscricao.py",
line 31, in index
f = crud.create(db.guys)
File "/var/www/web2py/gluon/tools.py", line 2960, in create
formname=formname,
File "/var/www/web2py/gluon/tools.py", line 2901, in update
detect_record_change = self.settings.detect_record_change):
File "/var/www/web2py/gluon/sqlhtml.py", line 1201, in accepts
self.vars.id = self.table.insert(**fields)
File "/var/www/web2py/gluon/dal.py", line 4429, in insert
return self._db._adapter.insert(self,self._listify(fields))
File "/var/www/web2py/gluon/dal.py", line 4422, in _listify
raise SyntaxError,'Table: missing required field: %s' % ofield.name
SyntaxError: Table: missing required field: userid
Both
db.guys.userid.default = auth.user_id
OR
db.guys.userid.compute = lambda f: uid
OR both together didn't worked.
When I set writable=True, the insertion was successful.
Then I changed the required=False:
Field('userid', db.auth_user, unique=True, required=False,
writable=False, readable=False)
And it worked correctly, saving the user. Is this the expected
behavior? Now its working, but I thing this really strange to me.
Allan Daemon
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GAT/CS/CM/ED/IT/MC/CA/SS/G/H d-- s++:+ a--(?) C++(++++) UBS+++>$ P++
Py+++>+++++$ L+ E---
W++ N++ o- K+ w++ !O M++ V-- PS++ PE- Y++ PGP++>+++ t 5? X R>+++ tv--() b DI+
D+ G++>++++ e+>++++ h r-->+++ y++**>$?
------END GEEK CODE BLOCK------