class Image(models.Model):
name = models.CharField(verbose_name='Image Name', max_length=50)
labID = models.ManyToManyField(Lab, blank=True, related_name='labID')
type = models.CharField(max_length=50)
def __str__(self):
return self.name
class Server(models.Model):
name = models.CharField(verbose_name='Server Name', max_length=50)
ipAddress = models.CharField(max_length=15)
maxCCU = models.IntegerField(default=0)
images = models.ManyToManyField(Image, blank=True, related_name='baseImage')
def __str__(self):
return self.ipAddress
class Childimage(models.Model):
name = models.CharField(verbose_name='Child Image Name', max_length=50)
parent = models.ForeignKey(Image, related_name='base')
server = models.ForeignKey(Server, related_name='server')
inUse = models.BooleanField()
rdpportnum = models.CharField(max_length=4)
def __str__(self):
return self.nameWhat I'm trying to do is have a web page that displays the number of parent images on a server by counting the child image. For example, I have a parent image called win2k12_excel and there are 12 child images of the parent win2k12_excel on the x.x.x.x server. I would like a web page that shows there are 12 win2k12_excel images on that server that would allow me to add or subtract the number on that server.I have created a view that does this and puts it into a multidimensional dictionary, but I cannot figure out how to get that to render properly in a template. Here is my view:def servers(request):
serverlist = Server.objects.all()
imagelist = Image.objects.filter(baseImage__in=serverlist).distinct()
childimagelist = defaultdict(list)
for server in serverlist:
for image in imagelist:
number = Childimage.objects.filter(parent__name__contains=image).filter(server__ipAddress__contains=server).count()
childimagelist[server].append({image: number})
childimagelist.default_factory = None
context = {
"server_list": serverlist,
"image_list": imagelist,
"child_list": childimagelist
}
return render(request, "administration/servers.html", context)No matter what I have tried so far, I cannot get this dictionary to render in a template. Is there a better way to do what I'm trying to do? Is there a proper way to get the dictionary parsed in the template? Any help would be appreciated.Oh, and I have search Google for weeks and tried all the suggestions I found. None of them worked. I can get the first part of the dictionary parsed, but no further than that. Here is the template code (server works, but for image, number in images.items, does not):{% for server, images in child_list.items %}
<h1>{{ server }}</h1>
{% for image, number in images.items %}
<h1>{{ number }}</h1>
{% endfor %}
{% endfor %}Any help at all would be appreciated...Thanks,Wolf
# views.py
from django.db.models import Count
def servers(request):
servers = Server.objects.all()
for server in servers:
server.child_images = server.images.aggregate(total=Count('base'))
return render(request, "administration/servers.html", {'servers': servers})
# template
{% for server in servers %}
<h1>Server: {{ server }}</h1>
No. images: {{ server.images.count }}<br/>
No. child images: {{ server.child_images.total }}
{% endfor %}
{% for server, images in child_list.items %}
<h1>{{ server }}</h1>
{% for image, number in images.items %}
<h1>{{ number }}</h1>
{% endfor %}
{% endfor %}
I've created project for managing virtual machines via libvirt using django few years ago. Also you can take a look at ganeti project. I'm ready to answer any question related to django and cloud computing.
Thanks, Serge
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d855fdde-6a52-48d9-9a87-02beadf59be7%40googlegroups.com.
{% for server, images in child_list.items %}
<h1>{{ server }}</h1>
{% for image in images %}
{% for key,val in image.items %}
<h1>{{ key }}: {{ val }}</h1>
{% endfor %}
{% endfor %}
{% endfor %}
I've created project for managing virtual machines via libvirt using django few years ago. Also you can take a look at ganeti project. I'm ready to answer any question related to django and cloud computing.
Thanks, Serge
--
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.