Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

representation format in web2py database

db.define_table('person', Field('name'), format='%(name)s')

What does this format do here?

like image 896
Jensen Avatar asked Jan 05 '12 15:01

Jensen


People also ask

How do I use web2py?

To start your Web2py web server, run the web2py.exe from your extracted files and it will then ask you to set up an administrative password to access your applications at a later point in time. Clicking on start server will open your web2py applications in the browser.

What is DAL Python?

web2py comes with a Database Abstraction Layer (DAL), an API that maps Python objects into database objects such as queries, tables, and records.

What is sqlform?

SQLFORM provides a high-level API for building create, update and delete forms from an existing database table. SQLFORM provides a high-level API for building create, update and delete forms from an existing database table.


1 Answers

The format argument is used to determine how fields in other tables that reference the 'person' table will be displayed. For example, if you define:

db.define_table('dog',
    Field('name'),
    Field('owner', db.person)

The 'owner' field is a reference field that references the 'person' table (i.e., it stores record id's of records from the 'person' table). In most cases, when you display data from the 'dog' table, you don't want to display the raw db.person record id that is stored in the 'owner' field because that doesn't have any meaning -- instead, it makes more sense to display the 'name' of the person. In web2py, the format attribute of the table enables this automatic substitution in both forms and tables.

When you create a SQLFORM based on the 'dog' table, it will automatically generate a drop-down list for the 'owner' field, and because of the format='%(name)s' argument to the 'person' table definition, the drop-down list will display db.person names instead of record id's (even though upon form submission, the 'owner' field will store the associated record id rather than the name).

Also, if you display records from the 'dog' table in a SQLTABLE or SQLFORM.grid, the 'owner' field will show the owner's name rather than the owner's record id.

See http://web2py.com/books/default/chapter/29/6#Record-representation.

like image 90
Anthony Avatar answered Oct 08 '22 09:10

Anthony