I have a Person
model and I am using a django form to edit another object with a foreign key to Person
. The person model has first_name
an
I have in mind 2 sugestions for you:
Sample:
persons = GetPersons().extra(select={'full_name': "concatenate( first, last) "} )
choices = persons.values_list('id', 'full_name')
and ...
Sample:
choices = [ ( p.id, '{0} {1}'.format( p.first, p.last ),) for p in GetPersons() ]
Edited 2018
Concat is now available as database function:
>>> from django.db.models import CharField, Value as V
>>> from django.db.models.functions import Concat
>>> persons = GetPersons().annotate(
... full_name=Concat(
... 'last', V(', '), 'first', V('.'),
... output_field=CharField()
... )
... )