Django Dying upon loop

56 views
Skip to first unread message

G Z

unread,
Jun 19, 2014, 11:16:56 PM6/19/14
to django...@googlegroups.com
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)

Tom Evans

unread,
Jun 20, 2014, 11:10:10 AM6/20/14
to django...@googlegroups.com
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.

G Z

unread,
Jun 20, 2014, 3:09:55 PM6/20/14
to django...@googlegroups.com, teva...@googlemail.com
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.

G Z

unread,
Jun 20, 2014, 3:44:26 PM6/20/14
to django...@googlegroups.com, teva...@googlemail.com
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)

G Z

unread,
Jun 20, 2014, 4:12:49 PM6/20/14
to django...@googlegroups.com, teva...@googlemail.com
shouldn't this work? After doing some more research into making django queries faster and more efficient I came across, http://tech.yipit.com/2013/01/20/making-django-orm-queries-faster/
I'm using thier method from what I understand you can selec_related to a primary key field and query like such, and it is the same as doing a join building a query set based on your parameters.


my html is 

    {% for item in customer %}
    <tr> 
        <td>{{ item.customer_id }}</td>
        
        <td><a href="{{ item.customer_id }}">{{ item.customer_name }}</a>
        {{ vm_groups.customer_id.vm_group_name }}
        {{ vms.vm_group_id.vm_name }}
        
        
        </td>
    </tr>
    {% endfor %}


my view code:

def portal(request):
    
    query_results = Customer.objects.all()
    vm_groups = Vm_group.objects.all().select_related('customer_id') 
    vms =  Vm.objects.all().select_related('vm_group_id')   


    context = Context({'customer': query_results, 'vm_groups':vm_groups, 'vms':vms,

George Silva

unread,
Jun 20, 2014, 4:16:49 PM6/20/14
to django-users, teva...@googlemail.com
You can use the raw() function to create a adhoc query to the database. Check the documentation and write proper sql code. It will be much more efficient.


--
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...@googlegroups.com.
To post to this group, send email to django...@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/f71cbe94-f7a0-4375-8d62-3468997a2aec%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
George R. C. Silva
SIGMA Consultoria
----------------------------

Jangita Nyagudi

unread,
Jun 21, 2014, 7:53:17 AM6/21/14
to django...@googlegroups.com

Can you post the two models? Are they related? You'd rather loop in the template in my opinion...

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages