SQLFORM.grid with joined tables - choose which table to view/edit by default

442 views
Skip to first unread message

Thomas Sitter

unread,
Nov 24, 2015, 1:30:35 PM11/24/15
to web2py-users
Hello,

I have two tables that I'd like to join in a SQLFORM.grid. When viewing or editing an entry in the grid I'd like to edit the cheque table, but by default the customer table is selected. How can I change this? I've recreated the relevant code below.

#db.py

db
.define_table('customer',
 
Field('firstname', label = "First Name"),
 
Field('lastname', label = "Last Name"),
)


db
.define_table('cheque',
 
Field('customer_id', 'reference customer'),
 
Field('cheque_sent', 'boolean'),
 
Field('cheque_value', 'double'),
)


#default.py

def test_view():
  query
= (
       
(db.cheque.customer_id == db.customer.id) &
       
(db.cheque.cheque_sent == True)
   
)

  fields
= [
      db
.customer.lastname,
      db
.cheque.cheque_sent,
 
]

  cheques
= SQLFORM.grid(query=query, fields=fields)

 
return dict(cheques=cheques)


How do I change the behaviour of the grid to bring up a form for the cheques table rather than customer?

Jim S

unread,
Nov 24, 2015, 2:44:18 PM11/24/15
to web2py-users
You can use the field_id parameter on the SQLFORM.grid call to specify the primary key field from the table that you want to be primary.


-Jim

Thomas Sitter

unread,
Nov 24, 2015, 2:55:08 PM11/24/15
to web2py-users
Thanks that's exactly what I was looking for!
Reply all
Reply to author
Forward
0 new messages