Opened 16 years ago
Closed 10 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 , 16 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 16 years ago
comment:3 by , 15 years ago
This is a related issue: #12889 Errors when using character fields for aggregation
Version 0, edited 15 years ago by (next)
comment:4 by , 15 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:5 by , 15 years ago
| Type: | → Bug |
|---|
comment:6 by , 15 years ago
| Severity: | → Normal |
|---|
comment:9 by , 10 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
Can't reproduce on master. Type is <class 'decimal.Decimal'> in both cases.
Note:
See TracTickets
for help on using tickets.
Related discussion:
http://groups.google.com/group/django-developers/browse_frm/thread/e87425876b758f83