Прошу прощения, возможно не правильно вопрос задал.. но суть ниже.
Имеется модель:
class CityManager(models.Manager):
def with_dencity(self):
return self.aggregate(dencity=Sum(F('population') /F('area'),output_field=FloatField()))
class CountryManager(models.Manager):
def with_num_cities(self):
return self.aggregate(num_cities=Count('cities'))
def with_biggest_city_size(self):
return self.aggregate(biggest_city_size=Max('cities__area'))
class Country(models.Model):
objects = CountryManager()
name = models.CharField(
verbose_name='Название',
max_length=255
)
def __str__(self):
return '{}'.format(self.name)
class City(models.Model):
objects = CityManager()
country = models.ForeignKey(
Country,
verbose_name='Страна',
related_name='cities',
on_delete=models.CASCADE
)
name = models.CharField(
verbose_name='Название',
max_length=255
)
population = models.FloatField(
verbose_name='Население'
)
area = models.FloatField(
verbose_name='Площадь'
)
def __str__(self):
return '{} {} {}'.format(self.name, self.population, self.area)
Пытаюсь выполнить запросы:
>>> c = City.objects.filter(name='Москва').aggregate(dencity=Sum(F('population') /F('area'),output_field=FloatField()))
>>> c
{'dencity': 10.0}
>>> c = City.objects.filter(name='Москва').with_dencity()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'with_dencity'
>>>
>>>
>>>
>>> c = Country.objects.filter(name='США').aggregate(num_cities=Count('cities'), biggest_city_size=Max('cities__area'))
>>> c
{'biggest_city_size': 700000.0, 'num_cities': 3}
>>> c = Country.objects.filter(name='США').with_num_cities().with_biggest_city_size()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'QuerySet' object has no attribute 'with_num_cities'
>>>
Если вместо фильтра использовать Get, то тоже выдает ошибку:
>>> c = Country.objects.get(name='США').with_num_cities().with_biggest_city_size()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Country' object has no attribute 'with_num_cities'
>>>
>>>
>>>
>>> c = City.objects.get(name='Москва').with_dencity()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'City' object has no attribute 'with_dencity'
>>>
>>>
>>> c = Country.objects.get(name='США').with_num_cities()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Country' object has no attribute 'with_num_cities'
>>>
Использовал для примера: https://docs.djangoproject.com/en/2.1/topics/db/managers/#creating-manager-wi...
Подскажите, пожалуйста, в чем может быть проблема и что подправить,чтоб указанные выше запросы выполнялись.