#10626: Default model ordering affects annotation query --------------------------------------+------------------------------------- Reporter: kmassey | Owner: Status: new | Milestone: 1.1 beta Component: ORM aggregation | Version: 1.1-beta-1 Keywords: values annotate ordering | Stage: Unreviewed Has_patch: 0 | --------------------------------------+------------------------------------- Example models without default orderings: {{{ class Author(models.Model): id = models.IntegerField(primary_key=True, help_text='Uniquely identifies an author.') name= models.CharField(max_length=200, help_text="Author's first name.")
class Book(models.Model): id = models.IntegerField(primary_key=True, help_text='Uniquely identifies a book.') title = models.CharField(max_length=200, help_text='The title of this book.') author = models.ForeignKey(Author, db_column='author_id', help_text='The author of this book.') price = models.FloatField( help_text='Price of the book.') inventory = models.IntegerField( help_text='Copies of book in the store.') }}} Use values() and annotate() to determine the number of books at each price. This works as I expected: {{{ >>> q = Book.objects.values('price').annotate(Count('price')) >>> list(q) [{'price': 7.9900000000000002, 'price__count': 2}, {'price': 9.9900000000000002, 'price__count': 1}] }}} Now add default orderings to the models: {{{ class Author(models.Model): id = models.IntegerField(primary_key=True, help_text='Uniquely identifies an author.') name= models.CharField(max_length=200, help_text="Author's first name.") class Meta: ordering = ['id'] class Book(models.Model): id = models.IntegerField(primary_key=True, help_text='Uniquely identifies a book.') title = models.CharField(max_length=200, help_text='The title of this book.') author = models.ForeignKey(Author, db_column='author_id', help_text='The author of this book.') price = models.FloatField( help_text='Price of the book.') inventory = models.IntegerField( help_text='Copies of book in the store.') class Meta: ordering = ['id'] }}} And repeat the query: {{{ >>> q = Book.objects.values('price').annotate(Count('price')) >>> list(q) [{'price': 9.9900000000000002, 'price__count': 1}, {'price': 7.9900000000000002, 'price__count': 1}, {'price': 7.9900000000000002, 'price__count': 1}] }}} This isn't what I expected. It took me a while to make the connection between the model default ordering and the behavior of the annotation query. Once I saw it, the fix was easy: {{{ >>> q = Book.objects.values('price').annotate(Count('price')).order_by() >>> list(q) [{'price': 7.9900000000000002, 'price__count': 2}, {'price': 9.9900000000000002, 'price__count': 1}] }}} I'm not sure if this should be a bug per se. If not, perhaps it could be noted in the aggregation documentation. Thanks, Kevin -- Ticket URL: <http://code.djangoproject.com/ticket/10626> 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-updates@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 -~----------~----~----~----~------~----~------~--~---