Table of contents
1.
Introduction
2.
Methods of CRUD
2.1.
Example of usage in the single controller function
3.
Record versions in web2py
4.
Frequently asked questions
4.1.
What is web2py?
4.2.
What are CRUD operations?
4.3.
What is the difference in commands to generate a raw SQL database?
4.4.
What is needed to make the record uniquely identifiable across databases?
4.5.
Which field is used to spot the duplication in the table?
5.
Conclusion
Last Updated: Aug 13, 2025
Easy

Methods of CRUD in web2py

Author Ankit Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Methods of CRUD may sound familiar to you, but most of us find web2py as alien in our world. So, let us start by addressing the elephant in the house “web2py”.web2py is the free, open-source web framework. It is a full-stack web framework used to develop highly secure, data-driven web pages. And where there is data, CRUD plays an important role. So, aren’t you interested in learning about the different CRUD methods in web2py? Let us discuss the methods of CRUD in detail. 

Introductory image

Methods of CRUD

Managing and updating data is an important part of the software industry. We do it by the four basic operations: create, read, update and delete, and the acronym CRUD simply represents it. We need to learn about the methods of CRUD so that we can use these operations effectively and manage data-driven applications in the software world.

Let us now see the signatures of methods of CRUD in web2py.

Methods of CRUD
crud.tables()
crud.create(table, next, onvalidation, onaccept, log, message, formname, **attributes)
crud.read(table, record)
crud.update(table, record, next, onvalidation, onaccept, ondelete, log, message, deletable)
crud.delete(table, record_id, next, message)
crud.select(table, query, fields, orderby, limitby, headers, **attr)
crud.search(table, query, queries, query_labels, fields, field_labels, zero, showall, chkall)


Let us look at the following meanings of the terms used in the above piece of code.

  • The table is either a DAL table or the name of the table the procedure should use.
     
  • The ids of the records that the method should operate on are record and record id.
     
  • The URL to redirect to the following success is then given. The id of the record that is now being created or edited will be used in place of the substring "[id]" if the URL contains it (use URL(..., URL encode=False) to prevent the brackets from being escaped).
     
  • The function of onvalidation is identical to those of FORM and SQLFORM.
     
  • Before redirection, but after the form submission is accepted and handled, a function called onaccept should be invoked. The form will be supplied as an argument when this is called.
     
  • Log is the message in the log. Variables of the form are seen in log messages for CRUD. 
     
  • Dictionary terms like "percent (id)s" (the message actually logged is log percent form.vars).
     
  • When a form is accepted, a flash message appears. **attributes add more dreck to the attribute. Arguments for the update keyword to be supplied to the function Object() { [native code] } of the SQLFORM.
     
  • When a record is deleted using an "update" form, ondelete is invoked rather than onaccept.
     
  • Deletable controls whether a delete option should be available on the "update" form
     
  • The search term to be used to choose records is the query.
     
  • Fields is a list of available fields.
     
  • The order in which records should be chosen is determined by orderby 
     
  • limitby chooses which entries from a certain range should be shown 
     
  • A dictionary called headers that is given to the SQLTABLE function Object() { [native code] } maps field names into header names searches a list like ['equals', 'not equal', 'contains'] that includes the permitted search techniques.

Let us now look at an example of usage in a single controller function.

Example of usage in the single controller function

## assuming db.define_table('person', Field('name'))
def people():
    form = crud.create(db.person, next=URL('index'),
                       message=T("record created"))
    persons = crud.select(db.person, fields=['name'],
                          headers={'person.name': 'Name'})
    return dict(form=form, persons=persons)


Here is another very generic controller function that lets you search, create and edit any records from any table where the table name is passed in the request.args(0):

def manage():
    table = db[request.args(0)]
    form = crud.update(table, request.args(1))
    table.id.represent = lambda id, row: \
       A('edit:', id, _href=URL(args=(request.args(0), id)))
    search, rows = crud.search(table)
    return dict(form=form, search=search, rows=rows)


The line table is visible. id.represent=... instructs web2py to display a link to the website rather than the actual text of the ID field and passes the ID as request. The new page becomes an update page when args(1) is used.

In the next section, we will be discussing the recording versions in web2py using CRUP operations.

Record versions in web2py

Database image

CRUD in web2py also offers to record the versions of a database. To do this simply we need to follow the given syntax of code. 

Let us suppose that we have a table named db.cntable

form = SQLFORM(db.cntable, myrecord).process(onsuccess=auth.archive)form = SQLFORM(db.cntable, myrecord).process(onsuccess=auth.archive)

form = crud.update(db.cntable, myrecord, onaccept=auth.archive)


When a record is updated, auth.archive creates a new table called db.cntable archive (whose name is taken from the name of the table to which it refers) and keeps a copy of the record (as it was before the update), together with reference to the current record, in the newly generated archive database.

References are never broken because the record is always updated (only its initial state is stored).

With this, we come to the end of this blog, and I hope you found this insightful.

Let us see some of the frequently asked questions related to this blog.

Frequently asked questions

What is web2py?

The web2py is an open-source framework that enables us to program dynamic content on the website using the python programming language.

What are CRUD operations?

Create, Read, Update and delete are the operations that are known as CRUD.

What is the difference in commands to generate a raw SQL database?

When a command starts with an underscore, it has to generate raw SQL.

What is needed to make the record uniquely identifiable across databases?

They must have a unique id(UUID), must have the last modification time for track in the most recent among multiple copies reference the UUID instead of the id.

Which field is used to spot the duplication in the table?

We will utilize the uuid field in a table to spot duplication if it is present. 

Conclusion

In this article, we extensively discussed how to handle methods of CRUD in web2py, which is an open-source language. Very fast, fluid in nature. We started with the introduction, and then we discussed Methods of CRUD and Recording older versions of the stored database. In the end, we discussed a few frequently asked questions.

Now you must be curious after knowing about web2py and how simple it is to learn. You can visit Basics of Python with Data Structures and Algorithms and Free Python Foundation with DS and Algo and start your journey.

Nevertheless, you may consider our paid courses to give your career an edge over others!

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

Happy Learning!

Thankyou image
Live masterclass