Hi, you can use aggregation queries to count the child images connected to 
each server:


# 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 %}

https://docs.djangoproject.com/en/1.10/topics/db/aggregation/

('child_image' would be a more suitable related_name than 'base')
 
Also, if server--image is a ManytoMany relation and an image can have many 
child images, the ForeignKey relation here between Childimage and Server 
would currently seem redundant/inaccurate. 

More broadly, if all child images are types of images, you could consider 
making ChildImage a subclass of Image instead, ie. class ChildImage(Image): 
which might help with querying, as then you could just count child images 
with server.childimage_set.count(), (if you removed the current 
related_name for Childimage.server).


On Tuesday, August 16, 2016 at 1:22:28 AM UTC+1, Wolf Painter wrote:
>
> Hey everyone, I didn't see any posts (or google search) that could 
> possibly answer my question, so I am here... I'm trying to create a way to 
> launch vm's on a Xen or VMWare server using a website. I have the python 
> file written on the Server and am currently creating a DJango app to manage 
> everything. I'm stuck in a big way. I  currently have models that track 
> servers, images and child images launched from the parent, but I can't 
> figure out how to display it properly in a template so i can update the 
> count (either add or subtract a number of VM's from a server). I'm new to 
> both Django and Python, so please excuse any ignorance here... Here are my 
> relevant models:
>
> 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.name
>
> What 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
>
>

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2b3c2b8d-042b-48d6-8a79-18e96d02ef73%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to