0
我想使用Django ORM運行以下SQL查詢。我不想使用原始的sql,因爲我想將它與另一個查詢連接起來。Django查詢:獲取按員工數量排序的狀態
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)
我試圖說明,但我沒有得到一個好的結果。你們能幫我嗎?
不知道你的模型... – Ngenator
在那裏,我添加了模型! –