Thank you, I will have to look into this. Django is a bit confusing to me as I'm just learning it, so I'm not as versed as I could be about all the options. What I'm trying to accomplish is a list of the copies of base images on each server, not sure if that makes sense. But here is an example:
win2k12_excel is a base image that can live on many servers, a copy of this image would be 0012_win2k12_excel which would live on only one server. I need to know how many copies of this base image live on each server and display that so I can either increase or decrease the count on each server. Does that make sense? I'm not sure if making ChildImage a subclass is what I want because the number of copies on each server can and will change frequently, as will the number 0012 in the earlier example. On top of the number of copies changing, images can and will be retired, so they will eventually be deleted and servers will change as they need updating. I will look into aggregate to see if it will work for what I'm trying to accomplish. Thank you! On Tuesday, August 16, 2016 at 11:00:09 PM UTC-7, Andrew Beales wrote: > > 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/d4d613e2-936b-4f15-a947-b9daa7c0fc06%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.