Altering reference table format in SQLFORM

26 views
Skip to first unread message

David Orme

unread,
Aug 11, 2020, 8:14:33 AM8/11/20
to web2py-users
I'm sure I'm missing something obvious but I am trying to alter the representation of a referenced field in a SQLFORM and failing hopelessly (Web2Py 2.20.4 on Python 3.7.7). The two tables involved are:

db.define_table('markers',
               
Field('first_name','string'),
               
Field('last_name','string'),
               
Field('email', 'string', requires=IS_EMAIL()),
                format
='%(first_name)s %(last_name)s')

db
.define_table('assignments',
               
Field('student_first_name','string', notnull=True),
               
Field('student_last_name','string', notnull=True),
               
Field('marker','reference markers'))

Most of the time, I want marker names as "John Smith", so have set that as the format in the model. However in a SQLFORM to create a new assignment, I want the dropdown to be "Smith, Bob (b...@smith.org)", to make it easier to find people. So:

def new_assignment():
   
    db
.markers._format = '%(last_name)s, %(first_name)s (%(email)s)'
   
    form
= SQLFORM(db.assignments)
   
   
if form.process().accepted:
        response
.flash = 'Assignment created'
        redirect
(URL('assignments'))
   
   
return dict(form=form)

But that does not change what appears in the SQLFORM dropdown menu for marker. If I swap the format string in the model, yup, exactly as expected, but it doesn't seem to be respecting the local redefinition.



villas

unread,
Aug 11, 2020, 11:07:36 AM8/11/20
to web2py-users
Off the top of my head, maybe these ideas would put you on a better track...  :)

1.  The reference field has a default validator, try changing it to something like this:
db.assignments.marker.requires = IS_IN_DB(db, 'markers.id', '%(last_name)s, %(first_name)s (%(email)s)')

2. Check out the .represent attribute


 

David Orme

unread,
Aug 11, 2020, 11:30:43 AM8/11/20
to web2py-users
Thanks! I had played with the represent attribute, with no success, but using that first option within the controller works. Changing the validator seems a bit over the top just to change the format, but it works!

AGRogers

unread,
Aug 12, 2020, 12:30:10 AM8/12/20
to web...@googlegroups.com
I just tried the format option change to the table to see if it worked for me... but it didn't. Thanks Villas for the 'requires' tip. I am sure i will need that knowledge at some point..

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/dc15ee08-5608-43b2-97fe-2c998aec5a9do%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages