Django和老式的SQL查询

时间:2013-01-04 09:25:21

标签: python sql django django-models python-2.7

我的模型如下:

class call_log(models.Model):
    call_from = models.CharField(max_length=200)
    call_to = models.CharField(max_length=200)
    direction = models.CharField(max_length=200)
    end_time = models.CharField(max_length=200)
    duration = models.CharField(max_length=200)
    total = models.DecimalField(max_digits=8, decimal_places=5, blank=True, default=0)
    rate = models.DecimalField(max_digits=8, decimal_places=5, blank=True, default=0)

它包含由(不同)号码和sip端点进行的每个单独呼叫的记录。

我要运行的SQL查询是:

SELECT DISTINCT(call_from) AS start, sum(total) AS total 
FROM polls_call_log GROUP BY start;

我如何在/为django写这个?

1 个答案:

答案 0 :(得分:0)

from django.db.models import Count
call_log.objects.values('call_from').annotate(Count('total'))