我有一点左连接问题。我有以下型号
class CommandInfo(models.Model):
server = models.ForeignKey(Server)
count = models.IntegerField(default=1)
ts = models.DateTimeField(auto_now=True)
class Server(models.Model):
name = models.CharField(max_length=100)
group = models.ForeignKey(ApplicationGroup, blank=True, default=0)
host = models.CharField(max_length=100)
ip = models.IPAddressField(db_index=True)
about = models.TextField()
firstTS = models.DateTimeField(auto_now_add=True)
lastTS = models.DateTimeField(auto_now=True)
processed = models.SmallIntegerField(max_length=1, default=0)
def __unicode__(self):
return self.host
我需要获取所有服务器实例,如果有的话,我需要将 CommandInfo 加入其中。
现在我正在使用原始 sql
from django.db import connection
cursor = connection.cursor()
cursor.execute("SELECT host,ts,count as host FROM servers_server LEFT JOIN cmds_commandinfo ON server_id=servers_server.id")
servers = cursor.fetchall()