A reference field in a web2py record contains a 0 before it is inserted despite it’s representation does not equal 0.
To explain in more detail, you see below a function and a table definition in a web2py model. The table contains bookings of staff members against an assignment of staff to a project element, “SB_AS_ref” refers to the corresponding record in “db.IT_C_AssignedStaff”, which in turn points to a staff record, and the staff record contains hourly rates needed for the value calculation (see "compute" in field SB_Value), as defined in calcSB_value(refTo_SB_AS_ref,hours).
This works during editing of a record, but when I add a record (in Data Administration or a grid, doesn’t make a difference), row['SB_AS_ref'] contains a zero, despite the fact that I’ve already selected a referred record “db.IT_C_AssignedStaff” in the front end (not so "hours", they correctly contain the value I’ve entered).
Which measures could I take to get the correct reference value before inserting a new record (or did I detect a bug)? Thanks in advance!
def calcSB_Value(refTo_SB_AS_ref,hours):
rows = \
db(db.IT_C_AssignedStaff.id==int(refTo_SB_AS_ref)
).select()
staffID = rows[0].AS_ST_ref
rows = db(db.IT_C_Staff.id==staffID).select()
hourlyRate = rows[0].ST_HourlyRate
return hours * hourlyRate
#----------------------------------------------
# MODEL
db.define_table('IT_C_StaffBooking',
Field('SB_AS_ref', db.IT_C_AssignedStaff,
required=True,
notnull=True,
label='Reference to staff assignment',
authorize=False,
uploadfield=False,
uploadseparate=False,
ondelete='CASCADE',
),
Field('SB_Date', type='date',
required=True,
notnull=True,
comment='Latest booking date',
label='Date of activity',
authorize=False,
uploadfield=False,
uploadseparate=False,
ondelete='CASCADE',
),
Field('SB_hours', type='integer',
required=True,
notnull=True,
comment='Latest booking date',
label='duration of activity',
authorize=False,
uploadfield=False,
uploadseparate=False,
default='0',
ondelete='CASCADE',
),
Field('SB_Value', type='decimal(10,2)',
compute=
lambda row: calcSB_Value(row['SB_AS_ref'],row['SB_hours']),
notnull=True,
comment='Will be calculated',
label='Value = duration * hourly rate',
authorize=False,
uploadfield=False,
uploadseparate=False,
default='0',
ondelete='CASCADE',
),
format='%(SB_AS_ref)s, %(SB_Date)s '
)