#12889: Using annotation unexpectedly returns DecimalFields as floats
------------------------------------------+---------------------------------
 Reporter:  KyleMac                       |       Owner:  nobody    
   Status:  new                           |   Milestone:            
Component:  Database layer (models, ORM)  |     Version:  SVN       
 Keywords:  annotate                      |       Stage:  Unreviewed
Has_patch:  0                             |  
------------------------------------------+---------------------------------
 Using the following models:


 {{{
 class Product(models.Model):
     name = models.CharField(_('name'), max_length=64)
     price = models.DecimalField(_('price'), max_digits=5,
 decimal_places=2)

 class Offer(models.Model):
     name = models.CharField(_('name'), max_length=64)
     start_time = models.DateTimeField(_('start time'))
     end_time = models.DateTimeField(_('end time'))
     var1 = models.DecimalField(_('var1'), max_digits=6, decimal_places=2)
     var2 = models.DecimalField(_('var2'), max_digits=6, decimal_places=2,
                                null=True, blank=True)
     contents = models.ManyToManyField(Product,
                                       verbose_name=_('contents'),
                                       related_name='offers')
 }}}

 Compare the following results:

 {{{
 now = datetime.utcnow()

 # Get all running offers
 offers = Offer.objects\
     .filter(start_time__lte=now, end_time__gt=now)
 # Will be a Decimal as expected
 print type(offers[0].var1)

 # Get only running offers that have some contents
 offers = Offer.objects\
     .filter(start_time__lte=now, end_time__gt=now)\
     .annotate(num_products=Count('contents'))\
     .exclude(num_products=0)
 # Will be a float
 print type(offers[0].var1)
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/12889>
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 django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to