Alexandr
Alexandr

Reputation: 53

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!

Upvotes: 0

Views: 138

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

You will have to do a raw query.

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

Upvotes: 1

Related Questions