web2py - require selected dropdown values validate from db

Tags: python web2py
By : Neveen
Source: Stackoverflow.com
Question!

i have a table member that include SQLField("year", db.All_years)

and All_years table as the following:

db.define_table("All_years",
  SQLField("fromY","integer"),
  SQLField("toY","integer")
  )

and constrains are :

db.member.year.requires = IS_IN_DB(db, 'All_years.id','All_years.fromY')

The problem is when I select a year from dropdown the value of year column is the id of year, not the year value e.g: if year 2009 has db id=1 the value of year in db equal=1 not equal 2009.

I don't understand why.

By : Neveen


Answers

I see your project is progressing well!

The validator is IS_IN_DB(dbset, field, label). So you should try:

db.member.year.requires = IS_IN_DB(db, 'All_years.id', '%(fromY)d')

to have a correct label in your drop-down list.

Now from your table it looks like you would rather choose an interval rather than just the beginning year, in that case you can use this:

db.member.year.requires = IS_IN_DB(db, 'All_years.id', '%(fromY)d to %(toY)d')

that will display, for example, "1980 to 1985", and so on.

By : RedGlyph


This video can help you solving your question :)
By: admin