[Answered ]-How to show name instad of id in django ForeignKey?

1👍

Here we can use getattr instead of value_from_object, which will return us the related instance of Person instead of an ID.

    def get_fields(self):
        return [(field.verbose_name, getattr(self, field.name)) for field in self.__class__._meta.fields]

example:

from base.models import Person
a = Person.objects.first()
a.get_fields()

Returns
[('ID', 1), ('name', 'Joe'), ('Very Important Name', <Person: Bob>)]

Now, we may access all of Bob‘s attributes in the template as well, if the need arose.

Leave a comment