I have a bit of a left join issue.. I have the following models
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
I need to grab all the server instances and left join the CommandInfo to it if there is one.
Right now I'm doing it in raw 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()