db.auth_user.give_me_data = Field.Method(lambda row: _give_me_data(row.auth_user))
def _give_me_data(user):
# calculation and obtention of data
# return data
from gluon.tools import Storage
CONFIG = Storage()
CONFIG.variable1 = 'value-variable-1'
CONFIG.variable2 = 'value-variable-2'
Straight to the point:I'm using lazy_tables=True and migrate=FalseIn my model, I have several Method Fields.Some of them use a function that is declared inside the same model, like this:
db.auth_user.give_me_data = Field.Method(lambda row: _give_me_data(row.auth_user))
def _give_me_data(user):
# calculation and obtention of data
# return dataSo first question: should I move declaration of _give_me_data function to a module?Or is it the same because I would have to import it anyway in the model?Just to remind, I'm using lazy_tables=False, so maybe the code isn't really executed until the table is called. I'm not sure about this.
Usage of Storage class objectsIn my models, I create some Storage objects, because it's really useful for setting and retrieving data from those objects. I use them like this:
from gluon.tools import Storage
CONFIG = Storage()
CONFIG.variable1 = 'value-variable-1'
CONFIG.variable2 = 'value-variable-2'I don't put much data inside them, just config variables.However, my question is: should I use other data type to storage those variables? Or it won't make any difference?
db.define_table('first_table', Field('field1'), Field('field2'))
db.first_table.field3 = Field.Method(lambda row: 'value for field3')
db.define_table('second_table', Field('field1'), Field('field2'))
db.second_table.field3 = Field.Method(lambda row: 'value for field3')
Thank you very much for the quick answer.Your first comment made me think about the "lazy_tables" with the definition of virtual method fields.If you say that doing db.tablename the table is no longer lazy, then it wouldn't make sense to use lazy_tables in a particular case where almost each table has a virtual method field, am I right?I mean, for example, if this is my model:db.define_table('first_table', Field('field1'), Field('field2'))
db.first_table.field3 = Field.Method(lambda row: 'value for field3')
db.define_table('first_table',
Field('field1'),
Field('field2'),
Field.Method('Field3', lambda row: 'value for field3'))
db.mytable.myfield = Field.Method('myfield', ...)
db.define_table('mytable', Field('field1'), Field('field2')) # defines the table
from myglobals import myfunction # imports global function
db.mytable._after_update.append(lambda s, r: myfunction(s, r)) # defines after update callback trigger
db.define_table('mytable',
Field('myfield'),
on_define=lambda table: table._after_update.append(lambda s, r: myfunction(s, r))
)
db.define_table('days', Field('name'), Field('plural_name'))
MONDAY = db.days[1]
TUESDAY = db.days[2]
WEDNESDAY = db.days[3]
THURSDAY = db.days[4]
FRIDAY = db.days[5]
SATURDAY = db.days[6]
SUNDAY = db.days[7]
from gluon.tools import Storage
MONDAY = Storage()
MONDAY.id = 1
MONDAY.name = 'monday'
MONDAY.plural_name = 'mondays'
Anthony, thank you very much for your help.
[...]
I'm tempted to ask one more thing that I'm wondering about:In my model, I have several tables (lets say about 8 or 9) that are used to store information about list of options.These tables only have a few records each, and they will never grow.For example, one table could be "days" (I don't have this table specifically, it's for the example).So, let's say the table "days" has a "name" field and a "plural_name" field.The table is populated with data only once, at application startup, and remains the same forever: "days" table will only have 7 records, one for each day of the week.Now, the part that I'm worried about is this: inside the same model, I store each record in a variable, like this:
db.define_table('days', Field('name'), Field('plural_name'))
MONDAY = db.days[1]
TUESDAY = db.days[2]
WEDNESDAY = db.days[3]
THURSDAY = db.days[4]
FRIDAY = db.days[5]
SATURDAY = db.days[6]
SUNDAY = db.days[7]I use that to be able to reference each day from the app code, but I've realised that in this way, the "days" table won't be lazy, because it's called to get the record.So my question is: what would be the correct approach for storing that kind of records in variables, without compromising lazy tables?
Now, the part that I'm worried about is this: inside the same model, I store each record in a variable, like this:
db.define_table('days', Field('name'), Field('plural_name'))
MONDAY = db.days[1]
TUESDAY = db.days[2]
WEDNESDAY = db.days[3]
THURSDAY = db.days[4]
FRIDAY = db.days[5]
SATURDAY = db.days[6]
SUNDAY = db.days[7]I use that to be able to reference each day from the app code, but I've realised that in this way, the "days" table won't be lazy, because it's called to get the record.
I've thouth of an answer myself, but I'm not sure.One possible approach would be to define Storage objects instead of pointing to the records.Because I already know the data of the days table, I can replace the previous code with this:
from gluon.tools import Storage
MONDAY = Storage()
MONDAY.id = 1
MONDAY.name = 'monday'
MONDAY.plural_name = 'mondays'Would it be better in terms of performance?Of course, I wouldn't have methods available at the row level, but I don't use them in this case.