Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6532581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:01:20+00:00 2026-05-25T10:01:20+00:00

I’m try to use a lambda to set a default value based on a

  • 0

I’m try to use a lambda to set a default value based on a value in
another table.

Here is my model:

db.define_table('trip_instance', timestamp, sos_table, 
    Field('trip_type_id',  db.trip_type, label='Trip Type', 
       widget=trip_select_widget, requires = IS_IN_DB(db, 'trip_type.id', '% 
       (name)s'), represent=lambda id:db.trip_type(id)['name']), 
    Field('total_slots', 'integer', default=lambda r: 
       db.trip_type(r.trip_type_id)['total_slots']), 

Locally this causes an error that says:

TypeError: <lambda>() takes exactly 1 argument (0 given) 

And on my server (Linode) there is no error but when I try and create
a new trip instance with sqlform the total slots field is pre-
populated with this:

<function <lambda> at 0x7f27684b7140> 

My lambda looks almost identical to the one that works fine for
represent in the trip_type_id field except I’m using the table instead
of the field. Is that what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:01:21+00:00Added an answer on May 25, 2026 at 10:01 am

    If you are using a callable (e.g., lambda) for a field default, I do not think it can take any arguments. Defaults are used to pre-populate form fields, so they should not depend on other field values in the record.

    An alternative is to use a computed field (though by default they do not appear in SQLFORMs). Another option is to handle the assignment at the form processing stage, possibly via an onvalidation function — something like this:

    form.accepts(...,onvalidation=lambda form:form.vars.update(
        total_slots=form.vars.total_slots or
        db.trip_type(form.vars.trip_type_id)['total_slots']))
    

    In the above code, if a value for total_slots is submitted, it is kept — otherwise, a default value is pulled from the trip_type table based on the value of the submitted trip_type_id.

    UPDATE:

    As noted in the comments, if you include the total_slots field in the form, if the field is left blank when the form is submitted, an empty value rather than the computed value will be stored in the database. To avoid this problem, you could specify an onvalidation function for the form that checks to see if form.vars.total_slots is empty, and if it is, delete form.vars.total_slots. In that case, because total_slots will not be included in the DB insert, the compute function will be called to fill in the value.

    If you want to completely avoid doing anything at the form processing stage, one other option might be to creat a custom validator for the total_slots field. The __init__ method of the validator can take request.vars as an argument so the submitted values for the record will be available to the validator when the form is submitted. When the validator is called (i.e., the __call__ method), it can check to see if the value of total_slots is empty, and if so, it can replace it with a value computed via the field’s compute function. With this setup, the standard compute function will handle direct DB inserts, and the validator will handle inserts via forms.

    Note, using the validator method, the value of total_slots will be computed based on the values in request.vars, which are the values of the submitted variables before validation. In this case, that shouldn’t be a problem, but more generally, if any of the field validators involve transformations, the values in request.vars will not exactly match the values ultimately inserted into the DB record.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.