just wondering about bulk insert and it's data type have a strange 
behaviour (error traceback occured), while the same code using DAL insert 
syntax and using import csv don't have a problem
*e.g.*
*models/db.py*
def __before_insert_attendance(f):
chosen_employee = db(db.employee.id == f['employee'] ).iterselect().first()
f['basic_salary'] = int(chosen_employee.basic_salary)
f['meal'] = int(chosen_employee.meal)
f['transport'] = int(chosen_employee.transport)
f['total'] = f['basic_salary'] + f['meal'] + f['transport']

db.define_table('attendance', 
Field('attendance_date', 'date'),
Field('employee', 'reference employee'), 
Field('basic_salary', 'integer'), 
Field('meal', 'integer'), 
Field('transport', 'integer'), 
Field('total', 'integer'), 
format = lambda r: '%s - %s %s' % (r.attendance_date, 
r.employee.first_name, r.employee.last_name) )

db.attendance._before_insert.append(__before_insert_attendance)
db.attendance.basic_salary.represent = lambda value, field: SPAN('%s' % 
(format(value, ",d").replace(",", ".") ) )

*controllers/install_demo.py*
def index():
if db(db.attendance).isempty():
db.employee.bulk_insert([
{'first_name' : 'User', 'last_name' : 'Staff', 
 'basic_salary' : 10000, 'meal' : 0, 'transport' : 0}, 
{'first_name' : 'User', 'last_name' : 'Packing', 
 'basic_salary' : 10000, 'meal' : 1500, 'transport' : 0}, ] )

#db.attendance.insert(attendance_date = "2016-11-01", employee = 1) # DAL 
insert syntax run well
db.attendance.bulk_insert([
  {'attendance_date' : '2016-11-01', 'employee' : 1}, 
  {'attendance_date' : '2016-11-02', 'employee' : 2}, ] )

*test.csv*
TABLE attendance
attendance_date,employee
2016-11-25,1

*error traceback using bulk insert when access attendance SQLFORM.grid*
db.attendance.basic_salary.represent = lambda value, field: SPAN('%s' % 
(format(value, ",d").replace(",", ".") ) )
ValueError: Unknown format code 'd' for object of type 'str'

i know i can simply comment the represent in the attendance table, but the 
same code using the different syntax have the different result.

any idea how to face it using web2py way?

thanks and best regards,
stifan

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to