简体   繁体   中英

how to execute subquery in Django orm?

django models.py

class cdr(models.Model):
    id = models.AutoField(primary_key=True, unique=True, verbose_name='id',)
    disposition = models.CharField(max_length=45, default='')
    did = models.CharField(max_length=50, default='')

    def __unicode__(self):
        return u'%s' % self.id

    class Meta:
        ordering=['-calldate']
        db_table = 'cdr'

MySQL Query:

select id, did as diddst, count(did) as count, (select count(did) from cdr where disposition='NO ANSWER' and did=diddst) as countnoanswer from cdr where did in (79244576674, 79244576619) group by did;

result

+------+-------------+-------+---------------+
| id   | diddst      | count | countnoanswer |
+------+-------------+-------+---------------+
| 1011 | 79244576619 |   218 |            71 |
| 1756 | 79244576674 |  1528 |           654 |
+------+-------------+-------+---------------+

how to execute this subquery in Django orm? help me please Django guys!

您将必须进行原始查询。

cdr.objects.raw('...')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM