开发者

proper Django ORM syntax to make this code work in MySQL

I have the following django code working on an sqlite database开发者_JAVA技巧 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


Ok finally managed to fix it with code below

 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=[a.latest_wslog_date for a 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 weird part was the original code worked as expected in an ubuntu machine running MySql but failed in a windows machine (also running MySql)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜