Answer the question
In order to leave comments, you need to log in
How to calculate the average price per room through aggregate?
There are three models
class Hotel(models.Model):
user = models.ForeignKey(MyUser, on_delete=models.CASCADE, verbose_name='Владелец')
title = models.CharField(max_length=100, verbose_name='Название')
def average_price(self):
return self.number_set.aggregate(Avg('get_price'))
class Number(models.Model):
hotel = models.ForeignKey(Hotel, default=1, on_delete=models.CASCADE, verbose_name='Отель')
title = models.CharField(max_length=100, verbose_name='Название')
def get_price(self):
return self.numberprice_set.all.aggregate(Avg('price'))
class NumberPrice(models.Model):
start = models.DateField(verbose_name='Начало периода')
finish = models.DateField(verbose_name='Конец периода')
price = models.IntegerField(null=True, blank=True, verbose_name='Цена')
number = models.ForeignKey(Number, on_delete=models.CASCADE, verbose_name='Номер')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question