[英]Django queryset annotation, charfield with choices and trying to get the display value
我模型的相关部分:
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())
您可以尝试上述代码片段,希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.