#28030: queryset.union count() method throws TypeError
-------------------------------------+-------------------------------------
Reporter: David | Owner: nobody
Binetti |
Type: Bug | Status: new
Component: Database | Version: 1.11
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 |
-------------------------------------+-------------------------------------
I'm not entirely certain how to create a test case, but here is the full
description:
{{{
# models.py
class Entity(models.Model):
"""Self-joined Model"""
....
parent = models.ForeignKey(
'self',
null=True,
blank=True,
related_name='children',
db_index=True,
on_delete=models.SET_NULL,
)
class Award(models.Model):
...
entity = models.ForeignKey(
'Entity',
related_name='awards',
on_delete=models.CASCADE,
)
}}}
With that, I'm using the new `union` method of 1.11 to create a queryset
combining all the all the `award` objects that relate to a particular
`entity` *and* the `award` objects of that `entity`'s children.
Thus, the queryset is:
{{{
e = Entity.objects.first()
qs = Award.objects.filter(entity=e).union(
Award.objects.filter(entity__parent=e)
)
}}}
This works, BTW, and produces the correct queryset results. However, when
I try to call count() I get...
{{{
>>>qs.count()
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'datetime.datetime'
}}}
with the following stack trace:
{{{
In [5]: qs.count()
---------------------------------------------------------------------------
TypeError Traceback (most recent call
last)
<ipython-input-5-e7e5018af10d> in <module>()
----> 1 qs.count()
/Users/dbinetti/.virtualenvs/barberscore-django/lib/python3.6/site-
packages/django/db/models/query.py in count(self)
361 return len(self._result_cache)
362
--> 363 return self.query.get_count(using=self.db)
364
365 def get(self, *args, **kwargs):
/Users/dbinetti/.virtualenvs/barberscore-django/lib/python3.6/site-
packages/django/db/models/sql/query.py in get_count(self, using)
496 obj = self.clone()
497 obj.add_annotation(Count('*'), alias='__count',
is_summary=True)
--> 498 number = obj.get_aggregation(using,
['__count'])['__count']
499 if number is None:
500 number = 0
/Users/dbinetti/.virtualenvs/barberscore-django/lib/python3.6/site-
packages/django/db/models/sql/query.py in get_aggregation(self, using,
added_aggregate_names)
482
483 converters =
compiler.get_converters(outer_query.annotation_select.values())
--> 484 result = compiler.apply_converters(result, converters)
485
486 return {
/Users/dbinetti/.virtualenvs/barberscore-django/lib/python3.6/site-
packages/django/db/models/sql/compiler.py in apply_converters(self, row,
converters)
817 value = row[pos]
818 for converter in convs:
--> 819 value = converter(value, expression,
self.connection, self.query.context)
820 row[pos] = value
821 return tuple(row)
/Users/dbinetti/.virtualenvs/barberscore-django/lib/python3.6/site-
packages/django/db/models/aggregates.py in convert_value(self, value,
expression, connection, context)
79 if value is None:
80 return 0
---> 81 return int(value)
82
83
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'datetime.datetime'
}}}
OK, I hope that's enough to go on. Can answer questions if necessary
--
Ticket URL: <https://code.djangoproject.com/ticket/28030>
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/051.8c050721b407214121b0e0e2e451fb2e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.