Here is the code:
variable=field_name
send_address_purchase=res_brw.variable (so i need res_brw.field_name)
How could i dynamically access field by variable value?
Don't use eval, use getattr instead:
variable = field_name
send_address_purchase = getattr(res_brw, variable)
Related
Currently I am working on a selenium project which I am integrated with Jenkins.I have stored Locators in a class. So my target is to take input from jenkins and use that variable as a key to get value from the
class Locators(object):
rundate = 'PREV' # This value is user input, either PREV or NOW
PREV = 'abcd'
NOW = 'bcd'
So I want to use it as:
Test = Locators()
Test.(Test.rundate)
Dotted attribute access always requires a valid identifier for the attribute name, not an arbitrary expression. Use getattr in other situations.
getattr(Test, Test.rundate)
I struggle with using variables (ultimately dictionary) for dynamically compose and access class attributes using getattr:
from gpiozero import PiStop
lights = PiStop('A+')
# working call: lights.red.on()
var = 'red.on'
getattr(lights(), var) # doesn't work - error
I cannot find proper syntax...
You have two attributes being accessed; lights.red is one such attribute, and on the result of that access, you then apply another attribute access, so <result>.on.
You need to use separate getattr() calls to achieve the same.
You could split on the '.' dot in var and apply each name separately, in a loop:
result = lights()
for name in var.split('.'):
result = getattr(result, name)
This allows for var to be set to any number of nested attributes.
I am trying to see if I can pass field name as a variable in get_or_create (since I have a function where the key in the kwargs can vary)
Like so:
def convert_value(cell_value, field_to_lookup):
rem_obj, created = Rem.objects.get_or_create(field_to_lookup=cell_value)
print ('created? ',created)
return rem_obj
The above wont work since it would look for 'field_to_lookup' as the key.
This post suggests using getattr but not sure if that'll be applicable in this case since I will again need to assign the output to a variable
This post helped. Now passing the field-value pair as dict which allows passing variables for field names. Here's the code:
def convert_value(cell_value, field_to_lookup):
rem_obj, created = Rem.objects.get_or_create(**{field_to_lookup:cell_value})
print ('created? ',created)
return rem_obj
Alternatively, I could directly just pass the dict to the function.
Is it possible to send a field name as a variable?
The following works (Note: myqso is a filtered queryset based on a model with a field called LocTypeNum):
myqsnew = myqso.annotate(newloctypenum=F('LocTypeNum')+10)
myqsop = [{'the_new_loctypenum':p.newloctypenum} for p in myqsnew]
But I want to send LocTypeNum in as a variable. Is there a better/faster method for making calculated fields and using variables as field names? Thanks!
In this is example LocTypeNum is just a string. So you can just replace with variable:
somefield = "some_field"
SomeModel.objects.all().aggregate(somename=Sum(F(somefield))
You can even path aggragetion name as varaiable:
SomeModel.objects.all().aggregate(**{somename: Sum(F(somefield)})
I have added the dictionary to the object with this:
dict['var1'] = {'value': 'tetsing'}
object.dict = dict
Now I am confused how to access the dictionary in view. Do I use:
object.dict['var1']['value'] or object.dict.var1.value
First of all you should to rename variable and attribute names so that they aren't in conflict with built-in names like dict.
Now, in normal Python code, use the first way.
However, if you are trying to access it from the Django template, use the second one.