Django

Code

Ticket #7791 (closed: fixed)

Opened 5 months ago

Last modified 3 months ago

len() on queryset with distinct ordered by child's field crashes

Reported by: isagalaev Assigned to: mtredinnick
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Given this model:

class Restaurant(models.Model):
    title = models.CharField(max_length=50)

class Pizza(models.Model):
    restaurant = models.ForeignKey(Restaurant)
    title = models.CharField(max_length=50)

and this data (one test record for each model):

[
  {
    "pk": 1, 
    "model": "main.restaurant", 
    "fields": {
      "title": "Karlo"
    }
  }, 
  {
    "pk": 1, 
    "model": "main.pizza", 
    "fields": {
      "title": "Margherita", 
      "restaurant": 1
    }
  }
]

calling len() on a queryset with distinct ordered by child's field:

len(Restaurant.objects.order_by('pizza__title').distinct())

gives this traceback:

Traceback (most recent call last):
  File "./manage.py", line 13, in <module>
    len(Restaurant.objects.order_by('pizza__title').distinct())
  File "/home/isagalaev/py/django/db/models/query.py", line 153, in __len__
    self._result_cache = list(self.iterator())
  File "/home/isagalaev/py/django/db/models/query.py", line 273, in iterator
    obj = self.model(*row[index_start:])
  File "/home/isagalaev/py/django/db/models/base.py", line 184, in __init__
    raise IndexError("Number of args exceeds number of fields")
IndexError: Number of args exceeds number of fields

Attachments

Change History

07/17/08 08:33:47 changed by isagalaev

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

BTW, it's len() that doesn't work. Actual querying for results works fine:

{{{#!python list(Restaurant.objects.order_by('pizzatitle').distinct()) }}}

07/17/08 12:11:00 changed by mtredinnick

  • stage changed from Unreviewed to Accepted.

Ivan, stop breaking my code! Guess we'll have to fix it now that somebody's noticed. :-(

07/17/08 12:47:19 changed by isagalaev

Heh :-)

The fun part is that I've later found out that in out code we'd better just order by a column in parent table instead of ordering by child with distinct :-). But still...

07/17/08 18:15:50 changed by korpios

  • cc set to korpios@korpios.com.

07/22/08 02:37:09 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [8052]) Fixed #7791 -- Fixed a really silly error I introduced in [7926]. :-(

07/22/08 02:59:11 changed by isagalaev

Malcolm, thanks you! This fix it's so in time (I was in a process of packaging our internal django-snapshot).

I'm also glad that it wasn't some deep fatal error of "it can't work at all like that" kind :-)

08/21/08 11:02:19 changed by korpios

  • cc deleted.

Add/Change #7791 (len() on queryset with distinct ordered by child's field crashes)




Change Properties
Action