Opened 11 years ago

Closed 8 years ago

#9365 closed Bug (worksforme)

Permutated columns when using Paginator with ValuesQuerySet

Reported by: micval@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Normal Keywords: paginator valuesqueryset
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Malcolm Tredinnick)

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.

Change History (4)

comment:1 Changed 11 years ago by Malcolm Tredinnick

Description: modified (diff)

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.

comment:2 Changed 11 years ago by Badri

Triage Stage: UnreviewedAccepted

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
<Page 1 of 1>
[<Document: foo1>, <Document: foo2>, <Document: foo3>]
<Document: foo3>
>>> x =[2]
>>> x.filesize
>>> x =[1]
>>> x.filesize
>>> x.filename

comment:3 Changed 8 years ago by Luke Plant

Easy pickings: unset
Resolution: worksforme
Severity: Normal
Status: newclosed
Type: Uncategorized

Can't reproduce this. Your example code gives the right answer - and not the second answer you posted, it includes 'filename' as it should.

Looking at Paginator, this bugs seems extremely unlikely, so I'm thinking something else must be going on.

comment:4 Changed 8 years ago by Luke Plant

Type: UncategorizedBug
Note: See TracTickets for help on using tickets.
Back to Top