[Django]-Python Django getattr(): attribute name must be string

5👍

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))

5👍

May be you are missing the attribute name.

I was getting the same error for this

price = models.DecimalField(15, 2,)

but when I modified the code like below error was resolved.

price = models.DecimalField(max_digits=15, decimal_places=2,)

Leave a comment