Reputation: 1
In my web2py application, I'm using an SQLFORM to add data into the db table called 'review'.
However, the created_by field is returning as none when it should display the logged in user's first name.
Any idea on what may be wrong and how I could rectify this?
Controller - default.py
def addReview():
response.menu = [
(T('Back to Reviews'), False, URL('default', 'index'))
]
review_form = SQLFORM(db.review)
if review_form.process().accepted:
session.flash = 'Review has been added!'
elif review_form.errors:
response.flash = 'Please review the form for errors'
return dict(review_form=review_form)
Model - db.py
db.define_table('product',
Field('name', requires=[IS_NOT_EMPTY(), IS_ALPHANUMERIC()]),
Field('description', 'text', requires=IS_NOT_EMPTY()),
Field('stock_level', 'integer')
)
db.define_table('review',
Field('product', 'reference product', writable=False, readable=False),
Field('title', requires=IS_NOT_EMPTY()),
Field('body', 'text', requires=IS_NOT_EMPTY()),
Field('created_by', 'reference auth_user', writable=False),
Field('added', default=request.now, writable=False)
)
Upvotes: 0
Views: 52
Reputation: 25536
Field('created_by', 'reference auth_user', writable=False)
The created_by
field is not writable, and no default is specified, so when an insert is made, there is no value for this field. If you want a value inserted, you must specify a default. Probably you want the currently logged in user:
Field('created_by', 'reference auth_user', writable=False, default=auth.user_id)
Upvotes: 0