问题
I'm using following code set to pull data from specific columns of the table...
cityList = City.objects.using(settings.DATABASE_CONF).filter(
status=1).values('city_name_en', 'city_id')
How can I set an alias for the city_name_en
column? I have two other columns with city_name_fr
city_name_de
but the client can understand only city_name
.
回答1:
I think giving alias in Django query is not possible. However you can refer to this answer.
回答2:
You can annotate the fields as you want, with the F expression:
from django.db.models import F
cityList = City.objects.using(settings.DATABASE_CONF).filter(status=1).values(
'city_name_en', 'city_id')
# use F expression to annotate with an alias
cityList = cityList.annotate(cityname=F('city_name_en'))
来源:https://stackoverflow.com/questions/18230691/alias-name-in-query-set