Reputation: 973
I have something like this in my_model.py
table_fields = [
'my_name-string',
'my_id-integer',
]
My goal is to create a table based on this information, so I tried:
fields_and_types_dict ={}
for tf in table_fields:
fields_and_types = tf.split('-')
fields_and_types_dict['%s' % fields_and_types[0]] = {}
fields_and_types_dict['%s' % fields_and_types[0]]['type'] = '%s' % fields_and_types[1]
db.define_table('my_table', fields_and_types_dict)
It´s not working properly and I guess I don´t have the right data-structure. Can anyone provide a hint how it must look like? Thank you in advance.
Solutions:
The most elegant solution to use is:
db.define_table('my_table', *[Field(*tf.split('-')) for tf in table_fields])
as suggested by Anthony.
The not-so-complex solution I used is:
tableDef = ['my_table']
for tf in table_fields:
name, type = tf.split("-")
tableDef.append(Field(name, type))
db.define_table(*tableDef)
as suggested by Vanojx1.
Upvotes: 1
Views: 40