There instead of looping through the queries i do it all in one loop but 
its still taking forever, how does django do the query, it seems like its 
doing a Cartesian product ? My server dies when ever I hit this page.

query_results = Customer.objects.all()
    customers = {}
    data = {}
    customer_names = {}
    vm_group_id = {}
    vm_groups = {}
    vm_names = {}

    for result in query_results:
       #get data
       customer_id = Customer.objects.values('customer_id')
       customer_name = Customer.objects.values('customer_name')     
       vm_group = Vm_group.objects.filter(customer=customer_id)
       vm_group_ids = Vm_group.objects.values('vm_group_id')
       vm_name = Vm.objects.values('vm_name')
       vms = Vm.objects.filter(vm_group_id=vm_group_ids)  
       
       
       #build dictonary

       vm_group_id['vm_group_id'] = vm_group_ids
       customer_names['customer_name'] = customer_name
       vm_names['vm_name'] = vm_name
       vm_groups['vm_group'] = vm_group
       data['info'] = (vm_group_id, customer_names, vm_name, vm_group)

       customers[customer_id] = data 

    context = Context({'customer': query_results, 'vm_group': vm_group, 
'vms': vms, 'customers':customers,
                                                })

    return render(request, 'portal.html', context)



On Friday, June 20, 2014 9:09:55 AM UTC-6, G Z wrote:
>
> Sad part is I can do it in python well, I just can't seem to grasp the way 
> django works i keep reading the damn documentation but I cant see any good 
> examples relating to what I have to do.
>
> On Friday, June 20, 2014 5:10:10 AM UTC-6, Tom Evans wrote:
>>
>> On Fri, Jun 20, 2014 at 12:16 AM, G Z <[email protected]> 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 [email protected].
To post to this group, send email to [email protected].
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/f71cbe94-f7a0-4375-8d62-3468997a2aec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to