<pre>
devices = Device.objects.filter(id__in=ids)
for i, device in enumerate(devices):
</pre>