I am using python, Django and get the following error:
getattr(): attribute name must be string
location: val = getattr(obj, field)
if field in headers:
if not isinstance(field, str):
val = getattr(obj, field)
else:
val = getattr(obj, field.LastName)
if callable(val):
val = val()
if type(val) == unicode:
val = val.encode("utf-8")
row.append(val)
I have tried many variation of code but all failed.
You can confirm the object type of field by using print(type(field)). It will likely not be a string considering the error.
Looking at your code, it looks like field will be an object with attributes that are strings, such as LastName. The line
val = getattr(obj, field)
would probably be better off reading
val = getattr(obj, field.someattribute)
If field.someattribute is not a string, you can cast it to string using
str(field.someattribute)
For a grand total of val = getattr(obj, str(field.someattribute))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With