On Fri, Jun 20, 2014 at 12:16 AM, G Z <zuk...@gmail.com> wrote:
> my django server dies whenever i login and access the following function:
>
> what is a better way to do what im doing. I need to loop through all of the
> vm_groups by customer id, and then loop through all of the vms and associate
> them with each other.
> The code below is what i have but its not working. django just kills its
> self.
>
> def portal(request):
>
>     query_results = Customer.objects.all()
>
>     for result in query_results:
>        customer_id = Customer.objects.values('customer_id')
>
>        vm_group = Vm_group.objects.filter(customer=customer_id)
>
>        for result in vm_group:
>           vm_group_ids = Vm_group.objects.values('vm_group_id')
>
>           for result in vm_group_ids:
>              vms = Vm.objects.filter(vm_group_id=vm_group_ids)
>
>
>     context = Context({'customer': query_results, 'vm_group': vm_group,
> 'vms': vms,
>                                                 })
>
>     return render(request, 'portal.html', context)
>

What is this code supposed to do?

You generate a "vm_group" for each customer, but then discard all but
the last one generated, which you put in your context.

Similarly, you generate a "vms" object for each vm group id in each vm
group in each customer, but then discard all but the last one.

Running queries inside loops inside loops inside loops is an extremely
inefficient way of querying the database, and this is ultimately why
it kills itself - it takes too long.

The purpose of the view is to efficiently generate data that you then
output in your template, but your view generates data inefficiently
that you discard. What do you want output in your template?

Cheers

Tom

PS:

I don't mean to discourage you, but looking back at your past posts,
they all seem to be variants of this exact question for the past
several months.

It might be worth doing some more straightforward exercises in python
so that you understand basic concepts like looping and variable
scoping, and when you get help on the mailing list, make sure you
understand why you are changing what you have been told to change -
ask questions of the people helping you if you don't know - otherwise
you are learning how to be a cargo cult programmer.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFHbX1L4pNhKa4qYs3qd8NH%3DSz9%3DHQGN%2BQEeg%3Deb2hHxOvgg9Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to