Django

Code

Ticket #9365 (new)

Opened 1 year ago

Last modified 1 year ago

Permutated columns when using Paginator with ValuesQuerySet

Reported by: micval@centrum.cz Assigned to: nobody
Milestone: Component: Database layer (models, ORM)
Version: 1.0 Keywords: paginator valuesqueryset
Cc: Triage Stage: Accepted
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description (Last modified by mtredinnick)

I have this model:

class Document(models.Model):
    filename    = models.CharField(max_length=1000)
    filesize    = models.IntegerField()
    keywords    = models.CharField(max_length=1000)
    content     = models.TextField()

When selecting documents I don't want the content field to be selected (for performance issues), so I use values like this:

docs = Document.objects.values('id','filename','filesize','keywords')

But when feeding this list directly to Paginator, the columns get permutated, e.g.: Paginator(docs,3,3,True).page(1).object_list gives:

[{'keywords': 161, 'id': 1, 'filesize': u'sample file'}, 
{'keywords': 162, 'id': 2, 'filesize': u'another sample file'}, 
{'keywords': 163, 'id': 3, 'filesize': u'sample file'}]

If I "evaluate" docs first, the columns get their correct values:

[{'keywords': u'sample file', 'id': 1, 'filesize': 161}, 
{'keywords': u'another sample file', 'id': 2, 'filesize': 162}, 
{'keywords': u'sample file', 'id': 3, 'filesize': 163}]

I use l = len(docs) workaround in my views, which does the trick, but this behaviour puzzles me a little bit.

Attachments

Change History

10/14/08 21:44:32 changed by mtredinnick

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

Fixed description. Please use the preview button to check your description in the future. Wiki syntax isn't like bulletin board syntax or whatever it was you were using and the text was unfortunately fairly unreadable.

10/19/08 12:43:07 changed by badri

  • stage changed from Unreviewed to Accepted.

Have you written any __unicode__ for your model? If so, you can paste here. It works fine for me. here is my output.

>>> docs
[<Document: foo1>, <Document: foo2>, <Document: foo3>]
>>> from django.core.paginator import Paginator
>>> paginator = Paginator(docs, 3, 3, True)
>>> paginator.num_pages
1
>>> paginator.page(1)
<Page 1 of 1>
>>> paginator.page(1).object_list
[<Document: foo1>, <Document: foo2>, <Document: foo3>]
>>> paginator.page(1).object_list[2]
<Document: foo3>
>>> x = paginator.page(1).object_list[2]
>>> x.filesize
163
>>> x = paginator.page(1).object_list[1]
>>> x.filesize
162
>>> x.filename
u'foo2'

Add/Change #9365 (Permutated columns when using Paginator with ValuesQuerySet)




Change Properties
Action