将Mysql查询转换为Django ORM

时间:2017-05-22 23:37:29

标签: python django view orm model

我正在使用Mysql视图然后我想在Django上使用mysql视图查询。但我不知道如何将查询转换为django orm ..

class HeartbeatHistory(models.Model):
index = models.AutoField(primary_key=True)
server = models.CharField(max_length=50, blank=True, null=True)
server_ip = models.CharField(max_length=50, blank=True, null=True)
eqp_id = models.CharField(max_length=50, blank=True, null=True)
eqp_ip = models.CharField(max_length=50, blank=True, null=True)
eqp_time = models.DateTimeField(blank=True, null=True)
server_time = models.DateTimeField(blank=True, null=True)
time_zone = models.CharField(max_length=50, blank=True, null=True)
time_gap = models.IntegerField(blank=True, null=True)
ftp_status = models.CharField(max_length=10, blank=True, null=True)
config = models.CharField(max_length=2000, blank=True, null=True)

class Meta:
    managed = False
    db_table = 'heartbeat_history'



> select 
`H`.`eqp_id` AS `eqp_id`,
`H`.`eqp_ip` AS `eqp_ip`,
(case when (`H`.`server_time` > (now() - interval 1 minute)) then '1' else '0' end) AS `isAlive`,
`H`.`eqp_time` AS `eqp_time`,
`H`.`server_time` AS `server_time`,
(case when (`H`.`ftp_status` = 'true') then 'OK' else 'NOT OK' end) AS `ftp_status`,
`H`.`config` AS `config` 
from (`collect_manager`.`heartbeat_history` `H` join 
(select `collect_manager`.`heartbeat_history`.`eqp_id` AS `eqp_id`,
max(`collect_manager`.`heartbeat_history`.`index`) AS `max_index` 
from `collect_manager`.`heartbeat_history` 
group by `collect_manager`.`heartbeat_history`.`eqp_id`) `H2` 
on((`H`.`index` = `H2`.`max_index`)))

感谢..

0 个答案:

没有答案