company_field_id = 'company_id'
db.define_table('bank', Field('bank_name', 'string', length=25, represent=lambda bank_name, row: bank_name.upper(),
required=True,
unique=True, notnull=True,
),
Field('default_branch', 'string', default='', length=25,
requires=[IS_EMPTY_OR(IS_MATCH('^[a-zA-Z][a-zA-Z ]*$'))]),
Field(company_id_field, 'reference company', notnull=True, ondelete=on_delete_cascade,
writable=False, readable=False), Field('request_tenant', default=auth.user_id, writable=False),
auth.signature, common_filter=lambda query: db.bank.company_id == get_default_company_id(),
format=lambda r: r.bank_name or 'No bank name!')
db.bank.bank_name.requires = [IS_NOT_EMPTY(error_message='You must enter a bank name'),
IS_MATCH('^[a-zA-Z][a-zA-Z ]*$',
error_message='Bank name can only contain alphabets (A-Z)'),
IS_NOT_IN_DB(db, db.bank.bank_name, error_message='Bank name must be unique.')]
db.bank._before_insert.append(
lambda f: f.update(
company_id=get_default_company_id()))
and here is my controller
@auth.requires_login()
def show():
db(db.company.id > 0).count() or redirect(URL('company', 'create'))
form = SQLFORM.smartgrid(db.bank)
return dict(form=form)and here is my view
{{extend 'layout.html'}}
{{=form}}Nothing I do gets this to work. Web2py always returns the error as per the screenshot attached. I've done a great deal of googling to no avail. Whats wrong?
That was just a typing error when I was writing the message here. It's actually correctly defined in my code.
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/fOfE_wct24Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
db.bank.common_filter = lambda query: db.bank.company_id == get_default_company_id()
seem to also work with no issues.
It appears that placing the 'common_filter' in the table definition together with the speical field 'request_tenant' is problematic. However, doing
db.bank.common_filter = lambda query: db.bank.company_id == get_default_company_id()
Field(company_id_field, 'reference company', default=get_default_company_id())db.define_table('company',
Field('name', 'string', length=20, represent=lambda name, row: name.upper(),
required=True,
unique=True, notnull=True,
),
Field('is_default', 'boolean', label=T('Default Company'),
default=False),
address,
auth.signature, common_filter=lambda query: db.company.created_by == auth.user_id,
format=lambda r: r.name.upper() or 'No name company')
db.company.name.requires = [IS_NOT_EMPTY(error_message='You must enter a company name'),
IS_MATCH('^[a-zA-Z0-9][a-zA-Z0-9 ,-.]*$'),
IS_NOT_IN_DB(db, db.company.name, error_message='This company name is in use')]
def set_all_default_company_to_false(fields):
if fields.get('is_default'):
rows = db(db.auth_user.id == auth.user_id).select(db.auth_user.ALL)
for row in rows:
for company in row.company.select(db.company.id, db.company.is_default):
if company.is_default:
db(db.company.id == company.id).update(is_default=False)
db.company._before_insert.append(lambda f: set_all_default_company_to_false(f))
db.company._before_update.append(lambda s, f: set_all_default_company_to_false(f))
def get_default_company_id():
rows = db(db.auth_user.id == auth.user_id).select(db.auth_user.ALL)
for row in rows:
for company in row.company.select(db.company.id, db.company.is_default):
if company.is_default:
return company.id
db.define_table('bank', Field('bank_name', 'string', length=25, represent=lambda bank_name, row: bank_name.upper(),
required=True,
unique=True, notnull=True,
),
Field('default_branch', 'string', default='', length=25,
requires=[IS_EMPTY_OR(IS_MATCH('^[a-zA-Z][a-zA-Z ]*$'))]),
Field(company_id_field, 'reference company', notnull=True, default=get_default_company_id(),
ondelete=on_delete_cascade,
writable=False, readable=False),
auth.signature, common_filter=lambda query: db.bank.company_id == get_default_company_id(),
format=lambda r: r.bank_name or 'No bank name!')
db.bank.bank_name.requires = [IS_NOT_EMPTY(error_message='You must enter a bank name'),
IS_MATCH('^[a-zA-Z][a-zA-Z ]*$',
error_message='Bank name can only contain alphabets (A-Z)'),
IS_NOT_IN_DB(db, db.bank.bank_name, error_message='Bank name must be unique.')]
1. | Traceback (most recent call last):
|
| web2py™ | Version 2.13.4-stable+timestamp.2015.12.26.04.59.39 |
|---|