Alias Name in Query Set

痞子三分冷 提交于 2019-12-07 13:04:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!