[Django]-Change field names in a Django query

8👍

You can use annotate() to add a new field to each item in the queryset. Then use values() so that you only return the columns you want in the union.

MyModel.objects.annotate(new_name=F('old_name').values('new_name', 'other_field').union(
    MyOtherModel.objects.annotate(new_name=F('other_old_name').values('new_name', 'other_field')
)

Leave a comment