且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何重命名Django中的值()中的项目?

更新时间:2023-11-28 21:07:46

这有点恶作剧,但您可以使用 extra 方法:

It's a bit hacky, but you could use the extra method:

MyModel.objects.extra(select={'renamed_value': 'cryptic_value_name'}).values('renamed_value')

这基本上在SQL中 SELECT cryptic_value_name AS renamed_value

另一个选项,如果你总是想要重命名的版本,但数据库具有隐含的名称,是使用新名称命名您的字段,但使用 db_column 以引用数据库中的原始名称。

Another option, if you always want the renamed version but the db has the cryptic name, is to name your field with the new name but use db_column to refer to the original name in the db.