>
> The proposal does not work as is. It looks like there is 
> no request.vars._formname.
>

How are you entering data into the table? If you are using SQLFORM, there 
should always be a hidden _formname field in the form. 
 

> P.S. : There is a small typo : request.vars._formname.startswith(...
>

Oops. 

However, with strong inspiration of your solution I made some additonal 
> tests and found a compute function that perfectly works :
>
> compute=lambda row: db.t_templates(request.vars.id).f_revision + 1 if (
> request.vars.id and db.t_templates(request.vars.id).f_revision) else 1,
>
>
That should generally work, though I would change it to

if (request.vars.id and db.t_templates(request.vars.id))

Otherwise you'll get an error if the query returns None and you then try to 
access the "f_revision" attribute of None. I had originally considered that 
simpler approach. The only danger is if for some reason a different form 
submission with request.vars.id comes in, and during that request you 
happen to insert or update the t_templates table as well -- in that case, 
the value inserted into f_revision will likely be based on the wrong 
record. I added a check for the _formname to ensure the submitted form is 
specifically for the t_templates table.

Anthony

 

> Super thanks Anthony. I'll use this solution as I prefer to have it on the 
> DAL.
>
> Le mardi 1 mai 2012 15:48:06 UTC+2, Anthony a écrit :
>>
>> I think the row object submitted to the compute function only includes 
>> the fields that are part of the update, so it won't include 
>> row['f_revision']. Instead, you could get the record id from request.vars 
>> and use that to query the current value of the f_revision field:
>>
>> db.define_table('t_templates', Field('name'),
>>     Field('f_template', 'upload', label=T('File'), notnull=True),
>>     Field('f_revision', 'integer', label=T('Revision'),
>>         compute=lambda: db.t_templates(request.vars.id).f_revision + 1 if 
>> (request.vars.id and
>>             request.vars._formname and request.vars_formname.startswith(
>> 't_templates')) else 1))
>>
>> Anthony
>>
>> On Tuesday, May 1, 2012 7:31:43 AM UTC-4, François Delpierre wrote:
>>>
>>> Hi,
>>>
>>> Here is exactly what I would like to have, but it does not compute on 
>>> update :
>>>
>>> db.define_table('t_templates',
>>>       Field('f_template', type='upload', label=T('File'),notnull=True),     
>>>                                                                        
>>>     
>>>       Field('f_revision', type='integer', label=T('Revision'),
>>>           default=1,
>>>           compute=lambda row: row['f_revision']+1 ,
>>>           ),
>>>       )
>>>
>>>

Reply via email to