[英]How do I convert mysql query to django model's ORM?
This is my mysql query. 这是我的mysql查询。
SELECT DeviceUID, Max(LogTime) , count(DeviceUID), Mode
FROM P2PLog.ConnectResult_Table group by DeviceUID;
How do I convert this code to ORM? 如何将此代码转换为ORM?
I try to this orm. 我尝试这个orm。 I need to select
Max(LogTime)
as column. 我需要选择
Max(LogTime)
作为列。 How do I do? 我该怎么办?
>>> ct = ConnectresultTable.objects
>>> aaa = ct.values('deviceuid', 'mode').annotate(items=Count('deviceuid'))
>>> print aaa.query
SELECT `ConnectResult_Table`.`DeviceUID`, `ConnectResult_Table`.`Mode`, COUNT(`C
onnectResult_Table`.`DeviceUID`) AS `items` FROM `ConnectResult_Table` GROUP BY
`ConnectResult_Table`.`DeviceUID`, `ConnectResult_Table`.`Mode` ORDER BY NULL
I finally get answer by myself. 我终于自己得到了答案。 Thanks everyone!
感谢大家!
>>> ct = ConnectresultTable.objects
>>> aaa = p2p_ct.values('deviceuid', 'mode').annotate(items=Count('deviceuid'),
new_status=Max('logtime'))
>>> print aaa.query
SELECT `ConnectResult_Table`.`DeviceUID`, `ConnectResult_Table`.`Mode`, COUNT(`C
onnectResult_Table`.`DeviceUID`) AS `items`, MAX(`ConnectResult_Table`.`LogTime`
) AS `new_status` FROM `ConnectResult_Table` GROUP BY `ConnectResult_Table`.`Dev
iceUID`, `ConnectResult_Table`.`Mode` ORDER BY NULL
$ python manage.py inspectdb
Run this command! $ python manage.py inspectdb
运行此命令!
$ python manage.py inspectdb > models.py Save your file using Unix.
class Person(models.Model):
id = models.IntegerField(primary_key=True)
first_name = models.CharField(max_length=70)
class Meta:
managed = False
db_table = 'CENSUS_PERSONS' ( Create unmanaged modes, managed=false)
$ python manage.py migrate ( And install this)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.