change the customer property to a ForeignKey

Everytime the warranty gets saved the customer gets
set to the one from the corresponding device
This commit is contained in:
Andreas Zweili 2020-03-01 11:16:36 +01:00
parent c159692445
commit b2bc1f5d88
1 changed files with 12 additions and 6 deletions

View File

@ -2,6 +2,7 @@ from django.db import models
from core.models import Category
from core.utils import td_format
from customers.models import Customer
from .device import Device
@ -14,11 +15,20 @@ class WarrantyType(Category):
class Warranty(models.Model):
customer = models.ForeignKey(Customer,
on_delete=models.CASCADE,
blank=True)
device = models.ForeignKey(Device, on_delete=models.CASCADE)
valid_from = models.DateField()
valid_until = models.DateField()
warranty_type = models.ForeignKey(WarrantyType, models.SET_NULL,
blank=True, null=True)
warranty_type = models.ForeignKey(WarrantyType,
models.SET_NULL,
blank=True,
null=True)
def save(self, *args, **kwargs):
self.customer = self.device.customer
super(Warranty, self).save(*args, **kwargs)
def __str__(self):
return str(self.device)
@ -26,10 +36,6 @@ class Warranty(models.Model):
class Meta:
verbose_name_plural = "Warranties"
@property
def customer(self):
return self.device.customer
@property
def duration_in_years(self):
delta = self.valid_until - self.valid_from