How can I use a property inside another property? Django
Question:
I have the following model with three properties to know the consumption according to the reading:
class ConsElect(models.Model):
pgd = models.ForeignKey(TipoPGD, on_delete=models.CASCADE, related_name='consumoelect')
data= models.DateField(default=datetime.now)
read_morning = models.IntegerField(default=0)
read_day = mod
def get_rm(self):
c_m = list(ConsElect.objects.filter(data__gt=self.data).values('read_morning')[0:1])
return ((c_m[0]['read_morning '] - self.read_morning ))
cons_m= property(get_rm)
def get_rd(self):
c_d = list(ConsElect.objects.filter(data__gt=self.data).values('read_day')[0:1])
return ((c_d[0]['read_day'] - self.read_day))
cons_d= property(get_rd)
def get_total(self): #It's not correct, I don't know how to implement it
return cons_d + cons_m #I need to get the sum of the above two properties
I need a third property in the model to store the sum of the two previous properties, how can I call the two previous properties inside the new property
Answers:
I suspect you want something like this.
class ConsElect(models.Model):
pgd = models.ForeignKey(TipoPGD, on_delete=models.CASCADE, related_name='consumoelect')
data= models.DateField(default=datetime.now)
read_morning = models.IntegerField(default=0)
read_day = mod
@property
def cons_m(self):
c_m = list(ConsElect.objects.filter(data__gt=self.data).values('read_morning')[0:1])
return ((c_m[0]['read_morning '] - self.read_morning ))
@property
def cons_d(self):
c_d = list(ConsElect.objects.filter(data__gt=self.data).values('read_day')[0:1])
return ((c_d[0]['read_day'] - self.read_day))
def get_total(self):
return self.cons_d + self.cons_m
A property
referenced via an instance attribute causes the getter for that property to be called. Typically, you use property
as a decorator as you rarely need access to the getter under a separate name. (If you need access to the getter at all, you can access it via the fget
attribute of the property
itself, e.g. cons_m.fget
.)
I have the following model with three properties to know the consumption according to the reading:
class ConsElect(models.Model):
pgd = models.ForeignKey(TipoPGD, on_delete=models.CASCADE, related_name='consumoelect')
data= models.DateField(default=datetime.now)
read_morning = models.IntegerField(default=0)
read_day = mod
def get_rm(self):
c_m = list(ConsElect.objects.filter(data__gt=self.data).values('read_morning')[0:1])
return ((c_m[0]['read_morning '] - self.read_morning ))
cons_m= property(get_rm)
def get_rd(self):
c_d = list(ConsElect.objects.filter(data__gt=self.data).values('read_day')[0:1])
return ((c_d[0]['read_day'] - self.read_day))
cons_d= property(get_rd)
def get_total(self): #It's not correct, I don't know how to implement it
return cons_d + cons_m #I need to get the sum of the above two properties
I need a third property in the model to store the sum of the two previous properties, how can I call the two previous properties inside the new property
I suspect you want something like this.
class ConsElect(models.Model):
pgd = models.ForeignKey(TipoPGD, on_delete=models.CASCADE, related_name='consumoelect')
data= models.DateField(default=datetime.now)
read_morning = models.IntegerField(default=0)
read_day = mod
@property
def cons_m(self):
c_m = list(ConsElect.objects.filter(data__gt=self.data).values('read_morning')[0:1])
return ((c_m[0]['read_morning '] - self.read_morning ))
@property
def cons_d(self):
c_d = list(ConsElect.objects.filter(data__gt=self.data).values('read_day')[0:1])
return ((c_d[0]['read_day'] - self.read_day))
def get_total(self):
return self.cons_d + self.cons_m
A property
referenced via an instance attribute causes the getter for that property to be called. Typically, you use property
as a decorator as you rarely need access to the getter under a separate name. (If you need access to the getter at all, you can access it via the fget
attribute of the property
itself, e.g. cons_m.fget
.)