db.define_table Advanced Field

32 views
Skip to first unread message

Alessandro Leonetti

unread,
Dec 9, 2015, 7:35:10 PM12/9/15
to web2py-users
Good evening,

I'm running into a problem quite uncomfortable but I'm pretty sure there is a fairly simple way to resolve it.
I defined the various tables within my database and put them in relation with each other (one to many, many to many etc.).
The problem is that when I insert a new record in a table that requires me to a field in another table, I have to specify that field in the record ID which will be linked to the record that I am creating.

Example:

Tables: Worker & Leader
I want to specify, creating a new record in the table worker who is the head of the worker that I am entering:

Name: BOB
Surname: DYLAN
Leader: 3 ----------------------> HOW DO I APPEAR TO A MENU 'CURTAIN FROM WHICH CAN CHOOSE ONE OF THE LEADERS ALREADY' INCLUDED IN THE DATABASE INSTEAD OF DOVER INSERT EXACT LEADER ID ???

Dovrò specificare qualcosa all'interno di 
db.define_table('Worker',
                Field('Name',requires=IS_NOT_EMPTY()),
                Field('Surname',requires=IS_NOT_EMPTY()),
                Field('LEADER', db.LEADER ..............................
               )
?????

Thanks for any answers, it would be great if you could help me, I'm losing a lot of time!

Best Regard and sorry for my disappointing english,
Alessandro

Massimiliano

unread,
Dec 10, 2015, 3:48:44 AM12/10/15
to web...@googlegroups.com
Take a look here:


You will find that you need to set the format value for the table leader just to inform web2py how to represent the record.

So in your example:

db.define_table(‘LEADER’,
                … your fields,
                format=‘%(yourfield)s’)


where yourfield is the LEADER’s field that you want to see instead the id.

Ciao

--
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 the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Massimiliano

Anthony

unread,
Dec 10, 2015, 12:12:56 PM12/10/15
to web2py-users
On Thursday, December 10, 2015 at 3:48:44 AM UTC-5, Massimiliano wrote:
Take a look here:


You will find that you need to set the format value for the table leader just to inform web2py how to represent the record.

So in your example:

db.define_table(‘LEADER’,
                … your fields,
                format=‘%(yourfield)s’)


where yourfield is the LEADER’s field that you want to see instead the id.

Note, by specifying the "format" argument in the LEADER table definition, that will give you a default IS_IN_DB validator as well as a default "represent" function for any reference field that references the LEADER table. The IS_IN_DB validator itself is responsible for generating the select widget in forms.

Alternatively, you could explicitly define your own IS_IN_DB validator and/or "represent" function. You could also explicitly define your own form widget by specifying the "widget" argument in the field definition.

All of these options are documented in the book.

Anthony
Reply all
Reply to author
Forward
0 new messages