我模型的相关部分:
class AnalyticRecord(models.Model):
APP = "APP"
WEB = "WEB"
DASH = "DASH"
SOURCE_CHOICES = (
(APP, "Mobile Application"),
(WEB, "Website"),
(DASH, "User Dashboard"))
user = models.ForeignKey(User, blank=True, null=True)
event = models.ForeignKey(Event)
source = models.CharField(max_length=25, choices=SOURCE_CHOICES)
我正在尝试运行聚合命令。它像这样正常工作:
data = event.analyticrecord_set.all().values("source").\
annotate(label=Concat("source", Value(None), output_field=CharField()))
但是,问题是注释
label
返回“ APP”,“ WEB”,“ DASH”而不是实际显示值。我知道我可以正常使用get_FOO_display()
,但是如何将显示值引入注释调用中?我正在寻找source
字段的显示值。谢谢! 最佳答案
queryset = event.analyticrecord_set.all().values("source").\
annotate(label=Concat("source", Value(None), output_field=CharField()))
for query in queryset:
print(queryset.model(source=query['source']).get_source_display())
您可以尝试上述代码片段吗,希望对您有所帮助
关于python - Django queryset批注,带有选择项并尝试获取显示值的charfield,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44087888/