Trying to use a variable as a field name in a table insert

55 views
Skip to first unread message

Peter

unread,
Jun 7, 2017, 10:42:28 PM6/7/17
to web2py-users

Good Morning.

I'm hoping there is a way to solve this...

This works fine for me...           

db.payment_allocations.insert(payment_ref=payment.id,
task_ref=record.id,
amount_allocated=allocation)

but I want to add a little abstraction like this...

ref_field = '%s_ref'  %  table_name        # where resulting field name will be either  'task_ref' or 'invoice_ref' (both fields are valid in the insert table).     
db.payment_allocations.insert(payment_ref=payment.id,
ref_field=record.id,
                              amount_allocated=allocation)

which currently gives an Attribute Error (taking ref_field literally and not substituting the variable's assigned value)

Is this possible to achieve what I want?
Any pointers would be appreciated.

Thanks
Peter

Anthony

unread,
Jun 7, 2017, 11:11:32 PM6/7/17
to web2py-users
That's not valid Python code, but you can do this:

db.payment_allocations.insert(**{'payment_ref': payment.id,
                                 ref_field
: record.id,
                                 
'amount_allocated': allocation})

Anthony

Peter

unread,
Jun 8, 2017, 2:49:17 PM6/8/17
to web...@googlegroups.com

Thanks for that Anthony!   (I have a feeling of deja vu about that one)

If you have time can you tell me what you mean...


That's not valid Python code

I'm not sure what it refers to and would like to understand where I am doing something wrong.


Regards
Peter

Anthony

unread,
Jun 8, 2017, 5:06:01 PM6/8/17
to web2py-users
This is not valid:

def some_function(the_real_parameter):
   
print the_real_parameter

parameter_name
= 'the_real_parameter'

some_function
(parameter_name='hello')

Anthony

Peter

unread,
Jun 9, 2017, 12:03:00 AM6/9/17
to web2py-users

I see... 

 TypeError: some_function() got an unexpected keyword argument 'parameter_name'



I was trying to avoid the if else but if it's the better way, so be it
and I have changed the code using a session cookie...

if session.allocations_table_name == 'task':

db.payment_allocations.insert(payment_ref=payment.id,                                           

                              task_ref=record.id,

                              amount_allocated=allocation)


elif session.allocations_table_name == 'invoice':

db.payment_allocations.insert(payment_ref=payment.id,

                              invoice_ref=record.id,

                              amount_allocated=allocation)


Thanks for talking the time to explain Anthony!

Peter





Anthony

unread,
Jun 9, 2017, 8:19:07 AM6/9/17
to web2py-users

Peter

unread,
Jun 12, 2017, 2:11:12 PM6/12/17
to web2py-users

Re:

What's wrong with this approach
db.payment_allocations.insert(**{'payment_ref': payment.id,
                                 ref_field
: record.id,
                                 
'amount_allocated': allocation})

To be honest I was trying to up my game with a bit of abstraction but haven't used the ** kwargs much (I don't think at all in web2py),
so with my limited understanding I thought it fell into the 'non pythonic' category you mentioned, though I do understand it is passing
a dictionary of values as opposed to over-riding an expected variable as in my earlier case.

Will give it a go so, thanks Anthony!

Thanks
Peter







Anthony

unread,
Jun 12, 2017, 3:49:10 PM6/12/17
to web2py-users
On Monday, June 12, 2017 at 2:11:12 PM UTC-4, Peter wrote:

Re:
What's wrong with this approach
db.payment_allocations.insert(**{'payment_ref': payment.id,
                                 ref_field
: record.id,
                                 
'amount_allocated': allocation})

To be honest I was trying to up my game with a bit of abstraction but haven't used the ** kwargs much (I don't think at all in web2py),
so with my limited understanding I thought it fell into the 'non pythonic' category you mentioned,

I don't think I said anything was "non pythonic" (I identified some invalid code -- but that code literally doesn't work at all, it is not merely non-Pythonic). The above code was my suggested (working) alternative, and I think the use of dictionary unpacking would be considered Pythonic.

Anthony

Peter

unread,
Jun 12, 2017, 8:04:26 PM6/12/17
to web2py-users

I don't think I said anything was "non pythonic" (I identified some invalid code -- but that code literally doesn't work at all, it is not merely non-Pythonic).
The above code was my suggested (working) alternative, and I think the use of dictionary unpacking would be considered Pythonic.
 
Absolutely no argument from me Anthony.
I simply didn't fully understand - that's on me - but thanks to you, taking the time to explain, I understand more now.         

If I haven't said it before...
I really do appreciate the time and effort you and others give to the forum and noobs like me - I'd be lost without it!

Incidentally, I may have another query to post, so hoping for some more support shortly!

Regards
Peter






Reply all
Reply to author
Forward
0 new messages