From 1dcec8e63a7c72501ffbbbf4f31fd909a9934107 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Mon, 25 Dec 2017 16:23:42 +0100 Subject: [PATCH] replace the Device model with a a GeneralDevice model This is required because otherwise when one gets all the Device objects the sub classes get pulled as well. With a general class we can use it really as a general device. --- docs/docs.org | 32 ++++++++++++++++++++++++-------- inventory/admin.py | 4 ++-- inventory/models.py | 8 ++++++++ inventory/views.py | 6 +++--- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/docs/docs.org b/docs/docs.org index 1340524..c83f71b 100644 --- a/docs/docs.org +++ b/docs/docs.org @@ -11,9 +11,7 @@ are very small because they exist mainly to keep the information unified. ** Device -The "Device" model represents a generel device about which I don't have -that much information. It might represent an IOT device or something -similarly closed. +The "Device" class is only used as an abstract class. #+BEGIN_SRC python :tangle ../inventory/models.py from django.db import models @@ -27,6 +25,21 @@ class Device(models.Model): return self.name #+END_SRC +** GeneralDevice + +The "GeneralDevice" model is used to describe devices which are very +simple or I don't have much control over. + +#+BEGIN_SRC python :tangle ../inventory/models.py +from django.db import models + + +class GeneralDevice(Device): + + def __str__(self): + return self.name +#+END_SRC + ** Weekday, DayOfMonth and Month These models contain all the days of the week the days in a month and @@ -311,7 +324,7 @@ We have to import each model we want to use. #+BEGIN_SRC python :tangle ../inventory/admin.py from django.contrib import admin -from inventory.models import (Device, Weekday, Month, RamType, Ram, +from inventory.models import (GeneralDevice, Weekday, Month, RamType, Ram, DiskType, DiskSize, Disk, Architecture, CpuManufacturer, Cpu, OperatingSystem, Raid, Computer, ComputerDiskRelation, @@ -357,7 +370,7 @@ In addition we have to define which admin form they should use if we want to use something different than the default one. #+BEGIN_SRC python :tangle ../inventory/admin.py :padline 2 -admin.site.register(Device) +admin.site.register(GeneralDevice) admin.site.register(Weekday) admin.site.register(Month) admin.site.register(RamType) @@ -377,17 +390,20 @@ admin.site.register(CronJob) * Views +Views are used to get information from the database and send them to +the templates. + #+BEGIN_SRC python :tangle ../inventory/views.py #!/usr/bin/python3 from django.shortcuts import get_object_or_404, render -from .models import (Device, Computer, CronJob, +from .models import (GeneralDevice, Computer, CronJob, ComputerRamRelation, ComputerDiskRelation, ComputerCpuRelation) def index(request): - device_list = Device.objects.all() + device_list = GeneralDevice.objects.all() computer_list = Computer.objects.all() return render(request, @@ -397,7 +413,7 @@ def index(request): def device_details(request, device_id): - device = get_object_or_404(Device, pk=device_id) + device = get_object_or_404(GeneralDevice, pk=device_id) return render(request, 'inventory/device_details.html', {'device': device}) diff --git a/inventory/admin.py b/inventory/admin.py index 4de4534..3828807 100644 --- a/inventory/admin.py +++ b/inventory/admin.py @@ -1,5 +1,5 @@ from django.contrib import admin -from inventory.models import (Device, Weekday, Month, RamType, Ram, +from inventory.models import (GeneralDevice, Weekday, Month, RamType, Ram, DiskType, DiskSize, Disk, Architecture, CpuManufacturer, Cpu, OperatingSystem, Raid, Computer, ComputerDiskRelation, @@ -30,7 +30,7 @@ class ComputerAdmin(admin.ModelAdmin): inlines = (CpusInLine, RamInLine, DiskInLine,) -admin.site.register(Device) +admin.site.register(GeneralDevice) admin.site.register(Weekday) admin.site.register(Month) admin.site.register(RamType) diff --git a/inventory/models.py b/inventory/models.py index c965d53..6999e76 100644 --- a/inventory/models.py +++ b/inventory/models.py @@ -8,6 +8,14 @@ class Device(models.Model): def __str__(self): return self.name +from django.db import models + + +class GeneralDevice(Device): + + def __str__(self): + return self.name + class Weekday(models.Model): name = models.CharField(max_length=50) diff --git a/inventory/views.py b/inventory/views.py index 660571d..fc7e551 100644 --- a/inventory/views.py +++ b/inventory/views.py @@ -1,13 +1,13 @@ #!/usr/bin/python3 from django.shortcuts import get_object_or_404, render -from .models import (Device, Computer, CronJob, +from .models import (GeneralDevice, Computer, CronJob, ComputerRamRelation, ComputerDiskRelation, ComputerCpuRelation) def index(request): - device_list = Device.objects.all() + device_list = GeneralDevice.objects.all() computer_list = Computer.objects.all() return render(request, @@ -17,7 +17,7 @@ def index(request): def device_details(request, device_id): - device = get_object_or_404(Device, pk=device_id) + device = get_object_or_404(GeneralDevice, pk=device_id) return render(request, 'inventory/device_details.html', {'device': device})