#13844: Errors when using character fields for aggregation
---------------------------------------------------+------------------------
          Reporter:  [email protected]              |         Owner:  nobody
            Status:  new                           |     Milestone:        
         Component:  Database layer (models, ORM)  |       Version:  1.2   
        Resolution:                                |      Keywords:        
             Stage:  Accepted                      |     Has_patch:  1     
        Needs_docs:  0                             |   Needs_tests:  0     
Needs_better_patch:  0                             |  
---------------------------------------------------+------------------------
Changes (by chronos):

 * cc: [email protected] (added)
  * has_patch:  0 => 1

Comment:

 I'm the guy at IRC. The proposed fix already works for me. I having
 troubles in get Max from a charfield.

 Given model:
 {{{
 class Link_A(models.Model):
     link = models.CharField(max_length=500)
     short_link = models.CharField(max_length=50)
 }}}

 I get following error when try to get Max of short_link:
 {{{
 In [5]: Link_A.objects.all().aggregate(Max('short_link'))
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 /home/felipe/projects/testproject/<ipython console> in <module>()

 /home/felipe/.py/2.6/lib/python2.6/site-
 packages/django/db/models/query.pyc in aggregate(self, *args, **kwargs)
     311                 is_summary=True)
     312
 --> 313         return query.get_aggregation(using=self.db)
     314
     315     def count(self):

 /home/felipe/.py/2.6/lib/python2.6/site-
 packages/django/db/models/sql/query.pyc in get_aggregation(self, using)
     371             (alias, self.resolve_aggregate(val, aggregate,
 connection=connections[using]))
     372             for (alias, aggregate), val
 --> 373             in zip(query.aggregate_select.items(), result)
     374         ])
     375

 /home/felipe/.py/2.6/lib/python2.6/site-
 packages/django/db/models/sql/query.pyc in resolve_aggregate(self, value,
 aggregate, connection)
     325         else:
     326             # Return value depends on the type of the field being
 processed.

 --> 327             return self.convert_values(value, aggregate.field,
 connection)
     328
     329     def get_aggregation(self, using):

 /home/felipe/.py/2.6/lib/python2.6/site-
 packages/django/db/models/sql/query.pyc in convert_values(self, value,
 field, connection)
     303         it can be overridden by Query classes for specific
 backends.
     304         """
 --> 305         return connection.ops.convert_values(value, field)
     306
     307     def resolve_aggregate(self, value, aggregate, connection):

 /home/felipe/.py/2.6/lib/python2.6/site-
 packages/django/db/backends/__init__.pyc in convert_values(self, value,
 field)
     443         # No field, or the field isn't known to be a decimal or
 integer

     444         # Default to a float

 --> 445         return float(value)
     446
     447     def check_aggregate_support(self, aggregate_func):

 ValueError: invalid literal for float(): c
 }}}

 and checking queries, I can see that querie are made:
 {{{
 In [6]: connection.queries
 Out[6]:
 [{'sql': 'SELECT MAX("webui_link_a"."short_link") AS "short_link__max"
 FROM "webui_link_a"',
   'time': '0.001'}]
 }}}

 That's is a small fix, but I think that need to check how act with others
 internal types, or default to float only if float(value) don't raise
 nothing and just return value if raise.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13844#comment:2>
Django <http://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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to