Introduction
Hello Ninjas,
Welcome to the new blog on SQLFORM.smartgrid in web2py, in which we will learn about SQLFORM.smartgrid, smartgrid signature, grid and smartgrid access control, and smartgrid plurals. It will be fascinating if you go through the SQLFORM.smartgrid in web2py because we will cover everything related to the topics I have mentioned above.

SQLFORM.smartgrid
An SQLFORM.smartgrid contains a grid and is designed to take as input only one table and select referencing tables.
For example, let us consider the following table structure:
db.define_table('department', Field('name'))
db.define_table('employee', Field('name'), Field('employee', 'reference parent'))
Using SQLFORM.grid, we can list all departments by-
SQLFORM.grid(db.department)
Also, we can list all employees by-
SQLFORM.grid(db.employee)
We can list all departments and employees in one table by
SQLFORM.grid(db.department,left=db.employee.on(db.employee.department==db.department.id))
We can put all the data in one object with SQLFORM.smartgrid that spawns both tables:
@auth.requires_login()
def manage():
grid = SQLFORM.smartgrid(db.department, linked_tables=['employee'])
return locals()
We have noticed the extra "employees" links. We can create the additional links using a regular grid, which would point to a different action. The smartgrid that are created automatically are handled by the same object.
We have also noticed that when we click on the "employees" link for a given department, we only get the list of employees for that department. If we try to add a new employee, the department value for the new employee is automatically set to the selected department. Also, the value of this field can be overwritten.
We can prevent this by making it read-only by doing the following:
@auth.requires_login()
def manage():
db.employee.department.writable = False
grid = SQLFORM.smartgrid(db.department, linked_tables=[‘employee'])
return locals()
All referencing tables are automatically linked if the linked_tables argument is not specified.
The listing tables should be linked explicitly to avoid accidentally exposing data.
Below is the following code that creates a compelling management interface for all tables in the system:
@auth.requires_membership('managers')
def manage():
table = request.args(0) or 'auth_user'
if not table in db.tables(): redirect(URL('error'))
grid = SQLFORM.smartgrid(db[table], args=request.args[:1])
return locals()
Now, let’s discuss another topic called the smartgrid signature.










