[Django] #35157: "AttributeError: 'QuerySet' object has no attribute 'copy'" combining FilteredRelation with subquery

6 views
Skip to first unread message

Django

unread,
Jan 30, 2024, 3:26:40 PM1/30/24
to django-...@googlegroups.com
#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.

Django

unread,
Jan 30, 2024, 3:58:14 PM1/30/24
to django-...@googlegroups.com
#35157: "AttributeError: 'QuerySet' object has no attribute 'copy'" combining
FilteredRelation with subquery
-------------------------------------+-------------------------------------
Reporter: Javier Ayres | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate

Comment:

Duplicate of #35135 (will be fixed in Django 5.0.2).
--
Ticket URL: <https://code.djangoproject.com/ticket/35157#comment:1>

Reply all
Reply to author
Forward
0 new messages