Table of contents
1.
Introduction
2.
SQLFORM.smartgrid
3.
Smartgrid Signature
4.
Grid and Smartgrid Access Control
5.
Smartgrid Plurals
6.
Frequently Asked Questions
6.1.
What is the web2py Framework?
6.2.
What do you mean by SQLFORM.smartgrid?
6.3.
What are the similarities between the web2py grid and smartgrid?
6.4.
What are the differences between the grid and smartgrid?
7.
Conclusions
Last Updated: Mar 27, 2024
Easy

SQLFORM.smartgrid in web2py

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Python Image

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'))
You can also try this code with Online Python Compiler
Run Code

 

Using SQLFORM.grid, we can list all departments by-

SQLFORM.grid(db.department)
You can also try this code with Online Python Compiler
Run Code


Also, we can list all employees by-

SQLFORM.grid(db.employee)
You can also try this code with Online Python Compiler
Run Code

 

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))
You can also try this code with Online Python Compiler
Run Code


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()
You can also try this code with Online Python Compiler
Run Code

 

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()
You can also try this code with Online Python Compiler
Run Code

Python Image

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()
You can also try this code with Online Python Compiler
Run Code

 

Now, let’s discuss another topic called the smartgrid signature.

Smartgrid Signature

Smartgrid Signature Image

Since the smartgrid takes the same arguments as a grid and some more with some cautions:

  • The first argument will be a table, not a query.
  • An extra argument constraint is a dictionary of 'tablename':query, which can be used to restrict further access to the records displayed in the 'tablename' grid.
  • There is an extra argument linked_tables that should be accessible via the smartgrid. linked_tables is a tablesnames of the table.
  • The divider allows specifying a character to use in the breadcrumb navigator; breadcrumbs_class will apply the class to the breadcrumb element.

Let us consider the previous grid :

grid = SQLFORM.smartgrid(db.department, linked_tables=['employee'])
You can also try this code with Online Python Compiler
Run Code

Table Image

It allows one to access a db.department and a db.employee. Apart from navigation controls, for each table, a smartgrid is nothing but a grid, which means that one smartgrid can create a grid for departments and one for an employee. 

We may pass different sets of parameters to these grids—for example, we can give different sets of searchable parameters.

For a grid, we may pass a boolean value:

grid = SQLFORM.grid(db.department, searchable=True)
You can also try this code with Online Python Compiler
Run Code

 

For a smartgrid, we may pass a dictionary of boolean values like:

grid = SQLFORM.smartgrid(db.department, linked_tables=['employee'],searchable= dict(department=True, employee=False))
You can also try this code with Online Python Compiler
Run Code

 

Therefore, in this way, we made the departments searchable, but the employee for each department are not searchable.

Now, let's discuss the grid and smartgrid access control.

Grid and Smartgrid Access Control

Free Access Image

We can integrate grid and smartgrid both with auth using explicit permission checking because it does not automatically enforce access control as crud do. :

grid = SQLFORM.grid(db.auth_user,editable = auth.has_membership('managers'),deletable =auth.has_membership('managers'))
You can also try this code with Online Python Compiler
Run Code

 

or it can also be written as-

grid = SQLFORM.grid(db.auth_user, editable = auth.has_permission('edit', 'auth_user'), deletable = auth.has_permission('delete', 'auth_user'))
You can also try this code with Online Python Compiler
Run Code


Lastly, we will discuss smartgrid plurals.

Smartgrid Plurals

Singular ImagePlural Image

The smartgrid needs both singular and plural and is the only object in web2py that displays the table name.

Let's take an example for better understanding.

Suppose one parent can have one "Child" or many "Children." Hence a table object needs to know its singular and plural names. As web2py normally guesses them, we can set them explicitly:

db.define_table('child', ..., singular="Child", plural="Children")
You can also try this code with Online Python Compiler
Run Code

or with:

db.define_table('child', ...)
db.child._singular = "Child"
db.child._plural = "Children"
You can also try this code with Online Python Compiler
Run Code

The plural and singular values are used by smartgrid to provide correct names for headers and links.

The End Image

Frequently Asked Questions

What is the web2py Framework?

web2py is a free and open-source web framework used for agile development which involves database-driven web applications and is written in Python.

What do you mean by SQLFORM.smartgrid?

An SQLFORM.smartgrid contains a grid and is designed to take as input only one table and to browse said table and select referencing tables.

What are the similarities between the web2py grid and smartgrid?

Both the grid and smart grid controls can browse, search, sort, create, update and delete records. Also, both can accept a table as the first parameter.

What are the differences between the grid and smartgrid?

The first argument in web2py grid is Table or query but in smartgrid the first argument is the table only.

Conclusions

At last, I would like to thank you for your dedication and patience in learning about these topics, as these are unique topics.

Let's end with the SQLFORM.smartgrid, in which we have covered different topics like smartgrid signature, smartgrid plurals, smartgrid access control, etc.

After reading about theDQLFORM.smartgrid in web2py, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. See web2pyWhat is web2pyweb2py Introductionweb2py Installation, and Creating a New Web2Py Application to learn.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass