class DeveloperManager models Manager def with_estates self developers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class DeveloperManager(models.Manager):
def with_estates(self):
developers = self.get_query_set().all()
d_ids = [d.id for d in developers]
all_estates = EstateItem.objects\
.prefetch_related('developer', 'district', 'preview_image',
'metro__metro', 'metro__mode')
estates = all_estates.filter(developer__in=d_ids, active=True)
for d in developers:
d.estates = filter(lambda e: e.developer_id == d.id, estates)
return developers