我正在使用 simple_history,因为我需要模型的历史数据。我有一个名为 po_staff 的模型,它有一个名为 pool_historicalpo_staff 的“历史”模型。现在我想获得一个基于 pool_historicalpo_staff 的查询集,其中包含每个员工的最后一条记录(我试图使其尽可能简单,因为真正的查询要复杂得多)。我目前正在使用 SQLLite
历史模型包含以下数据:
ID staff_nr valid staff_firstname staff_lastname
1 111 01/01/2014 Firstname1 Lastname1
2 3 01/01/2014 Firstname2 Lastname2
2 3 01/04/2014 Firstname2 Lastname2_new #Employee has changed his Lastname
此代码工作正常(按字母顺序排列):
qs = po_staff.history.raw ('SELECT * FROM pool_historicalpo_staff '
'WHERE clientID_id is %s and companyID_id is %s '
'GROUP BY id '
'ORDER BY staff_name, staff_surname ',
[client_id, company_id])
我必须使用“id”进行分组,因为可以从用户更改staff_nr(但它仍然是同一个员工)。
问题1:用户可以在alpabetical和staff_id之间选择结果的顺序
为了使这成为可能,我在查询中添加了第三个参数:
if order == 1:
order_by = "staff_name, staff_surname"
else:
order_by = "staff_nr"
qs = po_staff.history.raw ('SELECT * FROM pool_historicalpo_staff '
'WHERE clientID_id is %s and companyID_id is %s '
'GROUP BY id '
'ORDER BY %s ',
[client_id, company_id, order_by])
查询有效,但不是按字母顺序,而是按“id”排序。
问题 2:也必须为其他模型执行查询。只是模型和 order_by 会有所不同。我不想为每个模型编写几乎相同的查询,因此我尝试使其更灵活:
model = .... # instance of current model
order_by = .... # gets the choosen order for current model, p.e. "staff_name, staff_surname"
model_name = .... # gets the model name, p.e. "pool_historicalpo_staff"
qs = model.history.raw ('SELECT * FROM %s '
'WHERE clientID_id is %s and companyID_id is %s '
'GROUP BY id '
'ORDER BY %s ',
[model_name, client_id, company_id, order_by])
执行此查询时出现错误:
Request Method: GET
Request URL: http://127.0.0.1:8000/framework/21/?view=kanban
Django Version: 1.7
Exception Type: OperationalError
Exception Value: near "?": syntax error
Exception Location: c:\python34\lib\site-packages\django\db\backends\sqlite3\base.py in
execute, line 485
Python Executable: c:\python34\python.EXE
Python Version: 3.4.0
我希望我清楚地解释了一切。我是 django 的新手,这是我的第一个原始 SQL
非常感谢您的帮助!