Opened 7 years ago

Closed 7 years ago

#28030 closed Bug (duplicate)

queryset.union count() method throws TypeError

Reported by: David Binetti Owned by: nobody
Component: Database layer (models, ORM) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (1)

comment:1 by Simon Charette, 7 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #27995.

Note: See TracTickets for help on using tickets.
Back to Top