[Django]-Is it possible to reference a property using Django's QuerySet.values_list?

25👍

full name is not a field in the django model, it is not possible.
you can use list comprehensions

[person.fullname for person in Person.objects.all() ] 
👤zaca

21👍

values_list can only work on fields retrieved directly from the database. As zaca notes, you’ll need a list comprehension on the actual queryset:

[person.fullname for person in Person.objects.all()]

Don’t over-use values_list. It’s just meant as a means of limiting the db query, for when you know you’ll only ever need those particular fields. For almost all uses, getting the standard queryset is efficient enough.

9👍

Full name is not a field in the Django model, so you can use annotation like:

people = Person.objects.annotate(
    full_name=Concat(
        'first_name', 
        Value(' '), 
        'last_name'
    )
).values_list('full_name')

Leave a comment