I have a model Project
and i am getting the attributes of that with the following instr
attr = getattr(project, 'id', None)
project
is the instance, id
is the field and None
is the default return type.
My question is: what if I want to get the Foreign Key keys with this?
Get customer name
project.customer.name
How to get customer name with the above condition?
Already Tried
if callable(attr):
context[node][field] = '%s' % attr()
Current Code
context = {'project': {}}
fields = ('id', 'name', 'category', 'created_by', customer)
for field in fields:
attr = getattr(project, field, None)
if callable(attr):
context['project'][field] = '%s' % attr()
else:
context['project'][field] = attr
i need to adjust customer object here. that i can give something like customer__name
or customer.name
in my fields and it get populated with the name of the customer, but i am not sure.
project.customer.name
cutting it? – Phosphorname = getattr(project.customer, 'name', None)
would work? – Phosphor