#35157: "AttributeError: 'QuerySet' object has no attribute 'copy'" combining
FilteredRelation with subquery
-------------------------------------+-------------------------------------
Reporter: Javier | Owner: nobody
Ayres |
Type: Bug | Status: new
Component: Database | Version: 5.0
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 |
-------------------------------------+-------------------------------------
Starting with Django 5.0 I see the mentioned error in a query that
combines a FilteredRelation with a subquery in its condition. A new
project created with Django 5.0.1 and the following models will exhibit
this bug.
== models.py
{{{
from django.db import models
class Student(models.Model):
insert_time = models.DateTimeField(auto_now_add=True)
class StudentList(models.Model):
archived = models.BooleanField(default=False)
def __str__(self):
return self.name
class IncludedStudent(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE)
student_list = models.ForeignKey(StudentList,
on_delete=models.CASCADE)
insert_time = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('student', 'student_list')
}}}
== sample code to execute in the shell
{{{
Python 3.11.7 (main, Jan 17 2024, 06:37:55) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app.models import *
>>> from django.db.models import FilteredRelation, Q
>>> filter =
IncludedStudent.objects.filter(Q(student_list__archived=False)).values_list('id',
flat=True)
>>>
Student.objects.annotate(filteredincludedstudent=FilteredRelation('includedstudent',
condition=Q(includedstudent__id__in=filter)))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.11/site-
packages/django/db/models/manager.py", line 87, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-
packages/django/db/models/query.py", line 1630, in annotate
return self._annotate(args, kwargs, select=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-
packages/django/db/models/query.py", line 1676, in _annotate
clone.query.add_filtered_relation(annotation, alias)
File "/usr/local/lib/python3.11/site-
packages/django/db/models/sql/query.py", line 1682, in
add_filtered_relation
filtered_relation.condition = rename_prefix_from_q(
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-
packages/django/db/models/sql/query.py", line 120, in rename_prefix_from_q
[get_child_with_renamed_prefix(prefix, replacement, c) for c in
q.children],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-
packages/django/db/models/sql/query.py", line 120, in <listcomp>
[get_child_with_renamed_prefix(prefix, replacement, c) for c in
q.children],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-
packages/django/db/models/sql/query.py", line 100, in
get_child_with_renamed_prefix
rhs = get_child_with_renamed_prefix(prefix, replacement, rhs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-
packages/django/db/models/sql/query.py", line 108, in
get_child_with_renamed_prefix
child = child.copy()
^^^^^^^^^^
AttributeError: 'QuerySet' object has no attribute 'copy'
>>>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35157>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018d5c0d5bc5-01a9931e-948f-47b5-bd85-d3168dbae4ba-000000%40eu-central-1.amazonses.com.