Django ORM を使用して次の SQL クエリを実行したいと考えています。別のクエリと連結したいので、生のSQLを使用したくありません。
SELECT count(e.id), o.state
FROM core_employeemodel e, core_officemodel o
WHERE e.office_id = o.id
GROUP BY o.state
ORDER BY -count(e.id);
モデル:
class OfficeModel(Model):
address1 = CharField('address 1', max_length=50,)
address2 = CharField('address 2', max_length=50, blank=True)
city = CharField('city', max_length=50, db_index=True)
state = CharField('state', max_length=2, choices=STATE_CHOICES, db_index=True)
zip_code = CharField('zip code', max_length=50, db_index=True)
phone = CharField('phone', max_length=50,)
fax = CharField('fax', max_length=50, blank=True)
class EmployeeModel(Model):
first_name = CharField('first name', max_length=50, db_index=True)
last_name = CharField('last name', max_length=50, db_index=True)
picture = ImageField('picture', upload_to='employee_picture/', blank=True,)
fax = CharField('fax', max_length=50, blank=True)
email = EmailField('email', unique=True)
注釈を付けてみましたが、良い結果が得られませんでした。皆さん、私を助けてくれませんか?