How to perform a queryset in Django with a loop for in -
i use queryset obtain user list city:
usuarios = usuario.objects.filter(municipio=muni).exclude(user_id = ide)
so like:
[usuario: user1, usuario: user2, usuario: user3]
that need queryset obtain devices
for usuario in usuarios: devices = gcmdevice.objects.filter(user_id=usuario.user.id)
and last device last user(usuario):
[gcmdevice: samsung]
anybody know how can obtain devices users?
there more options. put dictionary user's id key:
for usuario in usuarios: devices[usario.user.id] = gcmdevice.objects.filter(user_id=usuario.user.id)
but bit demanding db, doing many queries. can obtain dcmdevice
records via single query:
user_ids = usuario.objects.filter(municipio=muni).exclude(user_id=ide).values_list('user_id', flat=true) devices = gcmdevice.objects.filter(user_id__in=user_ids)
Comments
Post a Comment