| author | ymh <ymh.work@gmail.com> |
| Fri, 04 May 2012 16:40:37 +0200 | |
| changeset 182 | 9d5eae13da59 |
| parent 11 | 143ab88d17f8 |
| permissions | -rw-r--r-- |
|
11
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
# -*- coding: utf-8 -*- |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
from django import forms |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
from django.db.models.query import QuerySet |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
|
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
|
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
class SortedMultipleChoiceField(forms.ModelMultipleChoiceField): |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
def clean(self, value): |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
queryset = super(SortedMultipleChoiceField, self).clean(value) |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
if value is None or not isinstance(queryset, QuerySet): |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
return queryset |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
object_list = dict(( |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
(unicode(key), value) |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
for key, value in queryset.in_bulk(value).iteritems())) |
|
143ab88d17f8
add ordered manytomany fields and indexing
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
return [object_list[unicode(pk)] for pk in value] |