== 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.
* 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>