Python Nested For Looping MultiTable Association Question

39 views
Skip to first unread message

G Z

unread,
May 7, 2014, 1:35:21 PM5/7/14
to django...@googlegroups.com
I'm new to django so im assuming there is an easier way to do this but in my html template im trying to do the following

I have a customer that has vmids associated with that and i want to match up every vm in the table vms by id to the vmids tag associated with the customer and display each one for each customer and eventually vmspecs are going to be tied to the vmid. I have my models.py and my index.html can anyone help me understand wahts wrong


<html>
<head>
<title>Customres</title>
</head>
<boody>
<p>Index Page</p>
        {% for e in customers %}
                {% for v in vms %}
                        {% if e.VMIDS = v.id %}
                                <div> {{ e.id }} - {{ e.NAME }} - {{ v.VMNAME }}     </div>
                        {% endif %}
                {% endfor %}
        {% endfor %}
</body>
</html>




class Customer(models.Model):
    NAME = models.CharField(max_length=200)
    WEBSITE = models.CharField(max_length=200)
    PHONE = models.CharField(max_length=200)
    EMAIL = models.CharField(max_length=200)
    ADDRESS = models.CharField(max_length=200)
    VMIDS = models.CharField(max_length=200)
    def __unicode__(self):
        return self.NAME
class Vms(models.Model):
    VMNAME = models.CharField(max_length=200)
    VMSTATUS = models.CharField(max_length=200)
    CUSTOMERID = models.ForeignKey(Customer)
    def __unicode__(self):
        return self.VMNAME
class Vmspecs(models.Model):
    CPUS =  models.CharField(max_length=200)
    CORES =  models.CharField(max_length=200)
    MEMORY =  models.CharField(max_length=200)
    HDSPACE =  models.CharField(max_length=200)
    OS =  models.CharField(max_length=200)
    UPTIME = models.CharField(max_length=200)
    VMID  = models.ForeignKey(Vms)
    CUSTOMERID = models.ForeignKey(Customer)
    def __unicode__(self):
       return self.VMID

G Z

unread,
May 7, 2014, 3:54:23 PM5/7/14
to django...@googlegroups.com
I don't know why this isn't working..


from django.shortcuts import render
from django.http import HttpResponse
from vmware.models import Customer
from django.shortcuts import render_to_response
from vmware.models import Vms

def index(request):
        customers = Customer.objects.all
        vms = Vms.objects.all
        ctx = { 'customers':customers, 'vms':vms}
        return render_to_response('index.html', ctx)

my views.py

the loop now reads

        {% for e in customers %}
                {% for e in vms %}
                        {% if e.VMIDS = e.id %}
                                <div> {{ e.id }} - {{ e.NAME }} - {{ e.VMNAME }}     </div>

G Z

unread,
May 7, 2014, 4:18:56 PM5/7/14
to django...@googlegroups.com
out of my stupidity i refined it a bit

        {% for e in customers %}
                {% for d in vms %}
                        {% if e.id = d.CUSTOMERID %}
                                <div> {{ e.id }} - {{ e.NAME }} - {{ d.VMNAME }}     </div>
                        {% endif %}
                {% endfor %}
        {% endfor %}

why doesn't this work.?

Rafael E. Ferrero

unread,
May 7, 2014, 4:19:51 PM5/7/14
to django...@googlegroups.com
2014-05-07 16:54 GMT-03:00 G Z <zuk...@gmail.com>:
I don't know why this isn't working..


from django.shortcuts import render
from django.http import HttpResponse
from vmware.models import Customer
from django.shortcuts import render_to_response
from vmware.models import Vms

def index(request):
        customers = Customer.objects.all()
        vms = Vms.objects.all
        ctx = { 'customers':customers, 'vms':vms}
        return render_to_response('index.html', ctx)

my views.py

the loop now reads

        {% for e in customers %}
                {% for e in vms %}
                        {% if e.VMIDS = e.id %}
                                <div> {{ e.id }} - {{ e.NAME }} - {{ e.VMNAME }}     </div>
                        {% endif %}
                {% endfor %}
        {% endfor %}



--
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/0e8117da-c6b0-4930-aebd-534124e025a5%40googlegroups.com.

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

G Z

unread,
May 7, 2014, 4:47:43 PM5/7/14
to django...@googlegroups.com
I read that but didn't quite get how to return the values the way I want.. 

Sylvain GROS-DESORMEAUX

unread,
May 7, 2014, 5:08:55 PM5/7/14
to django...@googlegroups.com
use == instead of = in your if statement.




--
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.

G Z

unread,
May 7, 2014, 5:47:36 PM5/7/14
to django...@googlegroups.com
ok so I added == to my if statement and independently verified that the values were displaying and are correct. So I was wrong though .id is not .CUSTOMERID because I returned self.name for the customer model it needs to match the strings

so i have 

        {% for e in customers %}
                {% for d in vms %}
                        {% if e.NAME == d.CUSTOMERID %}
                                <div> {{ e.id }} - {{ e.NAME }} - {{ d.VMNAME }$
                        {% endif %}
                        <br>
                        e.name - {{ e.NAME }}
                        d.customerid - {{ d.CUSTOMERID }} 

//I did this to validate I was getting data and the names match when they are displayed but the if statement doesn't display anything.

G Z

unread,
May 7, 2014, 5:48:02 PM5/7/14
to django...@googlegroups.com
by the way if someone could help me understand what the docs are saying about doing this in views that would be awesome.


On Wednesday, May 7, 2014 11:35:21 AM UTC-6, G Z wrote:

G Z

unread,
May 8, 2014, 2:19:55 PM5/8/14
to django...@googlegroups.com
   {% for e in customers %}
            {% for d in vms %}
                    {% if e.id = d.CUSTOMERID %}
                            <div> {{ e.id }} - {{ e.NAME }} - {{ d.VMNAME }}     </div>
                    {% endif %}
            {% endfor %}
    {% endfor %}

G Z

unread,
May 8, 2014, 2:20:16 PM5/8/14
to django...@googlegroups.com
outside the if statement the data appears..
Reply all
Reply to author
Forward
0 new messages