目前我正在使用 Django 和配置了 Djongo 的 MongoDB。
这些是我的 2 节课:
class Machine (models.Model):
machine_type = models.ForeignKey(MachineType, related_name ='machine_types', on_delete = models.CASCADE)
machine_customer = models.ForeignKey(Customer, related_name='machine_customers', on_delete =
models.CASCADE)
machine_serial_number = models.CharField(max_length = 255)
#
def __str__(self):
return self.machine_serial_number
class Part (models.Model):
part_id = models.CharField(max_length = 255)
machine_serial_number = models.ForeignKey(Machine, related_name='parts', on_delete = models.CASCADE)
machining_start = models.DateTimeField()
machining_end = models.DateTimeField()
quality = models.CharField(max_length = 3)
#
def __str__(self):
return self.part_id
我一直在尝试执行以下查询来查找每台机器上生产的不合格零件的数量。
results=Machine.objects.values('machine_serial_number')
.annotate(part_nok=Count('parts', filter=Q(parts__quality__icontains='NIO')))
但我不断收到以下错误消息:
COUNT(CASE WHEN "eda_main_part"."quality" iLIKE %(0)s THEN "eda_main_part"."id" ELSE NULL END) AS
"part_nok" FROM "eda_main_machine" LEFT OUTER JOIN "eda_main_part" ON ("eda_main_machine"."id" =
"eda_main_part"."machine_serial_number_id") GROUP BY "eda_main_machine"."machine_serial_number"
LIMIT 21
Version: 1.2.31
理想情况下,我想收到类似的东西:
{machine1:#111222, part_nok:121, part_ok:68, ... and so on }
当通过省略过滤器来执行以下查询时
results=Machine.objects
.values('machine_serial_number').annotate(part_nok=Count( 'parts'))
一切都按预期运行。
我正在使用以下工具:
sqlparse==0.2.4
djongo==1.2.31
Django==2.2.6
python 3.7.4
MongoDb 4.2