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.
This commit is contained in:
Andreas Zweili 2017-12-25 16:23:42 +01:00
parent 34b91ca1bf
commit 1dcec8e63a
4 changed files with 37 additions and 13 deletions

View File

@ -11,9 +11,7 @@ are very small because they exist mainly to keep the information unified.
** Device ** Device
The "Device" model represents a generel device about which I don't have The "Device" class is only used as an abstract class.
that much information. It might represent an IOT device or something
similarly closed.
#+BEGIN_SRC python :tangle ../inventory/models.py #+BEGIN_SRC python :tangle ../inventory/models.py
from django.db import models from django.db import models
@ -27,6 +25,21 @@ class Device(models.Model):
return self.name return self.name
#+END_SRC #+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 ** Weekday, DayOfMonth and Month
These models contain all the days of the week the days in a month and 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 #+BEGIN_SRC python :tangle ../inventory/admin.py
from django.contrib import admin 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, DiskType, DiskSize, Disk, Architecture,
CpuManufacturer, Cpu, OperatingSystem, CpuManufacturer, Cpu, OperatingSystem,
Raid, Computer, ComputerDiskRelation, 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. want to use something different than the default one.
#+BEGIN_SRC python :tangle ../inventory/admin.py :padline 2 #+BEGIN_SRC python :tangle ../inventory/admin.py :padline 2
admin.site.register(Device) admin.site.register(GeneralDevice)
admin.site.register(Weekday) admin.site.register(Weekday)
admin.site.register(Month) admin.site.register(Month)
admin.site.register(RamType) admin.site.register(RamType)
@ -377,17 +390,20 @@ admin.site.register(CronJob)
* Views * Views
Views are used to get information from the database and send them to
the templates.
#+BEGIN_SRC python :tangle ../inventory/views.py #+BEGIN_SRC python :tangle ../inventory/views.py
#!/usr/bin/python3 #!/usr/bin/python3
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from .models import (Device, Computer, CronJob, from .models import (GeneralDevice, Computer, CronJob,
ComputerRamRelation, ComputerRamRelation,
ComputerDiskRelation, ComputerDiskRelation,
ComputerCpuRelation) ComputerCpuRelation)
def index(request): def index(request):
device_list = Device.objects.all() device_list = GeneralDevice.objects.all()
computer_list = Computer.objects.all() computer_list = Computer.objects.all()
return render(request, return render(request,
@ -397,7 +413,7 @@ def index(request):
def device_details(request, device_id): 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', return render(request, 'inventory/device_details.html',
{'device': device}) {'device': device})

View File

@ -1,5 +1,5 @@
from django.contrib import admin 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, DiskType, DiskSize, Disk, Architecture,
CpuManufacturer, Cpu, OperatingSystem, CpuManufacturer, Cpu, OperatingSystem,
Raid, Computer, ComputerDiskRelation, Raid, Computer, ComputerDiskRelation,
@ -30,7 +30,7 @@ class ComputerAdmin(admin.ModelAdmin):
inlines = (CpusInLine, RamInLine, DiskInLine,) inlines = (CpusInLine, RamInLine, DiskInLine,)
admin.site.register(Device) admin.site.register(GeneralDevice)
admin.site.register(Weekday) admin.site.register(Weekday)
admin.site.register(Month) admin.site.register(Month)
admin.site.register(RamType) admin.site.register(RamType)

View File

@ -8,6 +8,14 @@ class Device(models.Model):
def __str__(self): def __str__(self):
return self.name return self.name
from django.db import models
class GeneralDevice(Device):
def __str__(self):
return self.name
class Weekday(models.Model): class Weekday(models.Model):
name = models.CharField(max_length=50) name = models.CharField(max_length=50)

View File

@ -1,13 +1,13 @@
#!/usr/bin/python3 #!/usr/bin/python3
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from .models import (Device, Computer, CronJob, from .models import (GeneralDevice, Computer, CronJob,
ComputerRamRelation, ComputerRamRelation,
ComputerDiskRelation, ComputerDiskRelation,
ComputerCpuRelation) ComputerCpuRelation)
def index(request): def index(request):
device_list = Device.objects.all() device_list = GeneralDevice.objects.all()
computer_list = Computer.objects.all() computer_list = Computer.objects.all()
return render(request, return render(request,
@ -17,7 +17,7 @@ def index(request):
def device_details(request, device_id): 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', return render(request, 'inventory/device_details.html',
{'device': device}) {'device': device})