class Group(models.Model):
name = models.CharField(max_length=25)
starosta = models.OneToOneField('Student', blank=True, null=True, related_name='group_st')
class Student(models.Model):
fio = models.CharField(max_length=50)
group = models.ForeignKey('Group', blank=True, null=True, related_name='students')
>>> for g in Group.objects.select_related('starosta').annotate(num_students=models.Count('students')).all():
... print g.name, g.starosta.fio, g.num_students
...
1 qwe 2
2 zxc 1
3 345 3
>>> from django.db import connection
>>> connection.queries
[{'time': '0.001', 'sql': u'SELECT "test_app_group"."id", "test_app_group"."name", "test_app_group"."starosta_id", COUNT(T3."id") AS "num_students", "test_app_student"."id", "test_app_student"."fio", "test_app_student"."group_id" FROM "test_app_group" LEFT OUTER JOIN "test_app_student" ON ("test_app_group"."starosta_id" = "test_app_student"."id") LEFT OUTER JOIN "test_app_student" T3 ON ("test_app_group"."id" = T3."group_id") GROUP BY "test_app_group"."id", "test_app_group"."name", "test_app_group"."starosta_id", "test_app_group"."id", "test_app_group"."name", "test_app_group"."starosta_id", "test_app_student"."id", "test_app_student"."fio", "test_app_student"."group_id"'}]
>>>