proper Django ORM syntax to make this code work in MySQL
- by gtujan
I have the following django code working on an sqlite database but for some unknown reason I get a syntax error if I change the backend to MySQL...does django's ORM treat filtering differently in MySQL?
def wsjson(request,imei):
wstations = WS.objects.annotate(latest_wslog_date=Max('wslog__date'),latest_wslog_time=Max('wslog__time'))
logs = WSLog.objects.filter(date__in=[b.latest_wslog_date for b in wstations],time__in=[b.latest_wslog_time for b in wstations],imei__exact=imei)
data = serializers.serialize('json',logs)
return HttpResponse(data,'application/javascript')
The code basically gets the latest logs from WSlog corresponding to each record in WS and serializes it to json.
Models are defined as:
class WS(models.Model):
name = models.CharField(max_length=20)
imei = models.CharField(max_length=15)
description = models.TextField()
def __unicode__(self):
return self.name
class WSLog(models.Model):
imei = models.CharField(max_length=15)
date = models.DateField()
time = models.TimeField()
data1 = models.DecimalField(max_digits=8,decimal_places=3)
data2 = models.DecimalField(max_digits=8,decimal_places=3)
WS = models.ForeignKey(WS)
def __unicode__(self):
return self.imei