When using django querysets. Is is possible to use the in operation, or another method, to maintain a one-to-one mapping between objects.
For example I would like:
id_list = [12,2,33]Foo.objects.filter(id__in=id_list)To preserve order and return
Nonewhen one of the ids is missing, eg:
ret = [Foo(id=12), None, Foo(id=33)]where
Foo(id=2)doesn't exist.
At present, the only method I have of doing this is to create an intermediate dictionary. For example:
map = {o.id: o for o in Foo.objects.filter(id__in=id_list)}
ret = [map.get(id, None) for id in id_list]
ret = [Foo.objects.get_or_none(id=id) for id in id_list]would work.