1
cxe2v 2013-08-04 18:31:43 +08:00
优先级这个没法设置吧,除了你给state赋予数字前缀,前缀就是优先级,或者来个state2字段给定优先级,当然灵活一点的是另建一张表,用state作为外键,另一个字段为优先级,这样就可以实现排序了
|
2
felix021 2013-08-04 19:31:08 +08:00 1
一句话:
tasks_list.sort(key=lambda x: {'todo': 1, 'finished': 2, 'delay': 3}[x.state]) |
3
z4none 2013-08-04 20:18:23 +08:00
如果是 MySQL :
select * from table where update_time='today' order by field (field, 'todo', 'finished', 'delay'); |
4
z4none 2013-08-04 20:19:46 +08:00
好吧 是 order by field (state, 'todo', 'finished', 'delay');
|
5
hahastudio 2013-08-05 11:19:05 +08:00
其实吧,你还是改SQL语句更划算= =我觉得= =
|
6
raquelken 2013-08-05 11:41:01 +08:00 1
你可以这样做 tasks_list=tasks.objects.filter(update_time='today').extra(select={"ordering": "case when state='todo' then 1 when state='finished' then 2 when state='delay' then 3 else 4 end"}).order_by('ordering')
但个人更建议你加字段 |