The above not handle bool correctly :
def controller_func():
...
field_type_mapping = \
{'id': 'int',
'reference': 'int',
'boolean': 'bool',
'string': 'str',
'decimal': 'decimal',
'date': 'date',
'datetime': 'datetime'}
for f in table_name_fields:
if db.table_name[f].type.split('(')[0] == 'decimal':
field_type = field_type_mapping['decimal']
elif db.table_name[f].type.split(' ')[0] == 'reference':
field_type = field_type_mapping['reference']
else:
field_type = field_type_mapping[db.table_name[f].type]
if f in request.vars:
db.table_name[f].default = request.vars[f]
table_name_fields[f] = request.vars[f]
# If there is value in request vars it has priority over
derived value
elif '_' + f in request.vars:
if field_type not in ('date', 'datetime', 'decimal', 'bool'):
db.table_name[f].default = convert(request.vars['_' +
f], field_type)
table_name_fields[f] = convert(request.vars['_' + f],
field_type)
elif field_type in ('date', 'datetime'):
if f == 'date':
db.table_name[f].default =
datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date()
table_name_fields[f] =
datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date()
elif f == 'datetime':
db.table_name[f].default =
datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S')
table_name_fields[f] =
datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S')
elif field_type == 'decimal':
db.table_name[f].default = Decimal(request.vars['_' + f])
table_name_fields[f] = Decimal(request.vars['_' + f])
elif field_type == 'bool':
if request.vars['_' + f] in ['True']:
request.vars['_' + f] = True
elif request.vars['_' + f] in ['False']:
request.vars['_' + f] = False
else:
request.vars['_' + f] = None
db.table_name[f].default = request.vars['_' + f]
table_name_fields[f] = request.vars['_' + f]
...
On Thu, Nov 3, 2016 at 11:24 AM, Richard Vézina <[email protected]
> wrote:
> I come up with this :
>
> def convert(value, type_):
> import importlib
> try:
> # Check if it's a builtin type
> module = importlib.import_module('__builtin__')
> cls = getattr(module, type_)
> except AttributeError:
> # if not, separate module and class
> module, type_ = type_.rsplit(".", 1)
> module = importlib.import_module(module)
> cls = getattr(module, type_)
> return cls(value)
>
> def controller_func():
> ...
> field_type_mapping = \
> {'id': 'int',
> 'reference': 'int',
> 'boolean': 'bool',
> 'string': 'str',
> 'decimal': 'decimal',
> 'date': 'date',
> 'datetime': 'datetime'}
>
> for f in table_name_fields:
> if db.table_name[f].type.split('(')[0] == 'decimal':
> field_type = field_type_mapping['decimal']
> elif db.table_name[f].type.split(' ')[0] == 'reference':
> field_type = field_type_mapping['reference']
> else:
> field_type = field_type_mapping[db.table_name[f].type]
> if f in request.vars:
> db.table_name[f].default = request.vars[f]
> table_name_fields[f] = request.vars[f]
> # If there is value in request vars it has priority over derived
> value
> elif '_' + f in request.vars:
> if field_type not in ('date', 'datetime', 'decimal'):
> db.table_name[f].default = convert(request.vars['_' + f],
> field_type)
> table_name_fields[f] = convert(request.vars['_' + f],
> field_type)
> elif field_type in ('date', 'datetime'):
> if f == 'date':
> db.table_name[f].default =
> datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date()
> table_name_fields[f] =
> datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date()
> elif f == 'datetime':
> db.table_name[f].default =
> datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S')
> table_name_fields[f] =
> datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S')
> elif field_type == 'decimal':
> db.table_name[f].default = Decimal(request.vars['_' + f])
> table_name_fields[f] = Decimal(request.vars['_' + f])
> ...
>
>
>
> But I am sure that there is something to parse request.vars and convert
> string vars into their specific type in web2py internal...
>
> Still interested in another cleaner solution...
>
> Thanks
>
> Richard
>
> On Tue, Nov 1, 2016 at 10:45 AM, Richard <[email protected]>
> wrote:
>
>> Hello,
>>
>> I have an high customized SQLFORM.factory() form... I set default value
>> for the update form for which I have no problem with values types as they
>> get out of the database... But in case I want to emulate keepvalue feature
>> with this form I need to pass value to request.vars which I need to set as
>> default... The problem is that all value are string when I get them and I
>> need to populate many fields and would greatly prefer not have to handle
>> each fields manually one by one but instead do it dynamically so if I had
>> field to the table I don't have to add it... So I am searching a way to
>> convert appropriately value for each field type... I could use the approach
>> proposed in the answer accepted hear : http://stackoverflow.com/que
>> stions/7402573/use-type-information-to-cast-values-stored-as-strings
>>
>> I there surely such a thing in the web2py internal... I thought
>> _filter_fields() would do it, but it only filter field that are member of a
>> table, it doesn't act over the value of the field... I thought of using
>> requires/validator, but I am not sure how to use them.
>>
>> Thanks
>>
>> Richard
>>
>>
>>
>> --
>> 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.
>>
>
>
--
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.