#30052: QuerySet's `only()` method does not work with annotated fields
-------------------------------------+-------------------------------------
Reporter: Kal Sze | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Database | Version: 2.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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).
--
Ticket URL: <https://code.djangoproject.com/ticket/30052>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/047.87dd913e9c316e8f8b01210f32659cb9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.