﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30052	QuerySet.only() doesn't support annotations	Kal Sze	nobody	"Example:


{{{
from django.db import models


class School(models.Model):
    full_name = models.TextField()


class Student(models.Model):
    full_name = models.TextField()
    date_of_birth = models.DateField()
    school = models.ForeignKey(School, on_delete=models.PROTECT)
}}}

And then:

{{{
from django.db.models import Subquery, OuterRef
from schools.models import School, Student

eldest_student_name = Student.objects.filter(school=OuterRef('id')).order_by('date_of_birth').values('full_name')[:1]

schools = School.objects.annotate(eldest_student_name=Subquery(eldest_student_name)).only('id', 'eldest_student_name')

for school in schools:
    # This line results in FieldDoesNotExist: School has no field named 'eldest_student_name'
    print(school.id, school.eldest_student_name)
}}}

Not sure if this is a bug or an (undocumented) limitation of `only()` (and maybe of `defer()`, by extension)."	Bug	closed	Database layer (models, ORM)	2.1	Normal	invalid			Unreviewed	0	0	0	0	0	0
