Opened 15 years ago

Closed 9 years ago

#12889 closed Bug (fixed)

Using annotation unexpectedly returns DecimalFields as floats

Reported by: KyleMac Owned by: Greg Wogan-Browne
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: annotate
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)

Change History (9)

comment:1 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Matthias Kestenholz, 14 years ago

This is a related issue: #13844 Errors when using character fields for aggregation

Last edited 9 years ago by Tim Graham (previous) (diff)

comment:4 by Greg Wogan-Browne, 14 years ago

Owner: changed from nobody to Greg Wogan-Browne
Status: newassigned

comment:5 by Luke Plant, 14 years ago

Type: Bug

comment:6 by Luke Plant, 14 years ago

Severity: Normal

comment:7 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 by Tim Graham, 9 years ago

Resolution: fixed
Status: assignedclosed

Can't reproduce on master. Type is <class 'decimal.Decimal'> in both cases.

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