# Create your models here.
class City(models.Model):
# ...
pass
class Person(models.Model):
# ...
hometown = models.ForeignKey(
City,
on_delete=models.SET_NULL,
blank=True,
null=True,
)
class Book(models.Model):
# ...
author = models.ForeignKey(Person, on_delete=models.CASCADE)
}}}
{{{
Person.objects.all().union(Person.objects.all()).select_related('hometown')
}}}
**Traceback:**
{{{
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/lib/python3.6/site-packages/django/db/models/query.py", line 226,
in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/lib/python3.6/site-packages/django/db/models/query.py", line 250,
in __iter__
self._fetch_all()
File "/lib/python3.6/site-packages/django/db/models/query.py", line
1121, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/lib/python3.6/site-packages/django/db/models/query.py", line 66,
in __iter__
rel_populator.populate(row, obj)
File "/lib/python3.6/site-packages/django/db/models/query.py", line
1748, in populate
if obj_data[self.pk_idx] is None:
IndexError: tuple index out of range
}}}
Also, on a different project with some more complicated models in the same
Django version the IndexError occurs earlier, I don't know why.
{{{
Traceback (most recent call last):
File "lib/python3.6/site-packages/IPython/core/formatters.py", line 224,
in catch_format_error
r = method(self, *args, **kwargs)
File "lib/python3.6/site-packages/IPython/core/formatters.py", line 702,
in __call__
printer.pretty(obj)
File "lib/python3.6/site-packages/IPython/lib/pretty.py", line 402, in
pretty
return _repr_pprint(obj, self, cycle)
File "lib/python3.6/site-packages/IPython/lib/pretty.py", line 697, in
_repr_pprint
output = repr(obj)
File "lib/python3.6/site-packages/django/db/models/query.py", line 226,
in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "lib/python3.6/site-packages/django/db/models/query.py", line 250,
in __iter__
self._fetch_all()
File "lib/python3.6/site-packages/django/db/models/query.py", line 1121,
in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "lib/python3.6/site-packages/django/db/models/query.py", line 62,
in __iter__
for row in compiler.results_iter(results):
File "lib/python3.6/site-packages/django/db/models/sql/compiler.py",
line 847, in results_iter
row = self.apply_converters(row, converters)
File "lib/python3.6/site-packages/django/db/models/sql/compiler.py",
line 830, in apply_converters
value = row[pos]
IndexError: list index out of range
}}}
Also reproduced it in 1.11
--
Ticket URL: <https://code.djangoproject.com/ticket/30456>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* version: 2.2 => master
* resolution: => invalid
Comment:
It's unsupported but doesn't raise an error yet. As
[https://docs.djangoproject.com/en/stable/ref/models/querysets/#union per
the documentation], ''"only LIMIT, OFFSET, COUNT(*), ORDER BY, and
specifying columns (i.e. slicing, count(), order_by(), and
values()/values_list()) are allowed on the resulting QuerySet."''. See
#27995 for a ticket to raise a helpful message about this.
--
Ticket URL: <https://code.djangoproject.com/ticket/30456#comment:1>