【发布时间】:2021-07-12 14:39:26
【问题描述】:
我希望在以下方面获得您的帮助: 我有两个模型父模型和一个子模型:
class Rate(models.Model):
RATE_VOLTAGE_CHOICES = (
(BAJA_TENSION, "Low tension"),
(MEDIA_TENSION, "Mid tension"),
(ALTA_TENSION, "High tension")
)
rate_type = models.CharField(max_length=1, choices=RATE_TYPE_CHOICES, default="0")
class ParentRate(models.Model):
rate = models.ForeignKey(Rate, on_delete=models.SET_NULL, blank=True, null=True
)
所以前端想要通过低张力、中张力和高张力“过滤”rate_type 选择作为字符串,他们发送字符串 所以我正在尝试使用 When Case 对象,以便我可以过滤:
rates = ParentRate.objects.annotate(rate_voltage=Case(
When(rate__rate_type=Rate.BAJA_TENSION, then=Value('Low tension')),
When(rate__rate_type=Rate.MEDIA_TENSION, then=Value('Mid tension')),
When(rate__rate_type=Rate.ALTA_TENSION, then=Value('High tension')),
default=Value('Low tension'),
)
)
但我收到以下错误:
django.core.exceptions.FieldError: Cannot resolve expression type, unknown output_field
【问题讨论】:
标签: python-3.x django postgresql django-models django-queryset