Opened 10 years ago

Closed 5 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: master
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


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,

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)\
# Will be a float
print type(offers[0].var1)

Change History (9)

comment:1 Changed 10 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:3 Changed 10 years ago by Matthias Kestenholz

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

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

comment:4 Changed 9 years ago by Greg Wogan-Browne

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

comment:5 Changed 9 years ago by Luke Plant

Type: Bug

comment:6 Changed 9 years ago by Luke Plant

Severity: Normal

comment:7 Changed 8 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 Changed 8 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:9 Changed 5 years ago by Tim Graham

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