﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17878	Recursive order_with_respect_to	eric@…	nobody	"Unsure of the version in which this occurs, as I'm running the development version. 

I have a simple a straightforward model that looks something like this:

{{{

class Section(models.Model):
    pass

class Passage(models.Model):
    section=models.ForeignKey(Section,related_name='passages')

    class Meta:
        order_with_respect_to = 'section'

ANSWERS = ((0,''),(1,'A'),(2,'B'),(3,'C'),(4,'D'),(5,'E'))
class Question(models.Model):
    passage=models.ForeignKey(Passage,related_name='questions')
    correct_answer = models.IntegerField(max_length=1,choices=ANSWERS)

    class Meta:
        order_with_respect_to = 'passage'
}}}

Note that I have omitted the properties of the model not pertinent to this question. Now, my problem is that although it is quite easy to get an ordered list of correct answers for a given passage: (p is a passage instance)


{{{
p.questions.values_list('correct_answer',flat=True)
}}}

It is actually much more difficult than you'd expect to get the same answer set, now for the entire section instead. Essentially, I want the ordering on the question model to look like this:


{{{
ordering = ['passage___order','_order']
}}}

instead of the assumed:


{{{
ordering = ['_order']
}}}

However, even if I try to set this manually in the Meta class for Question, the actual ordering of a fetch like this: (where s is an instance of a section)


{{{
Question.objects.filter(passage__section=s).values_list('correct_answer',flat=True)
}}}
 
The ordering that's returned here is quite useless, as it just sorts the questions based on the '_order' attribute with no regard to the fact that now it'd be much more useful to order by the parent passage's order_with_respect_to attribute first, and then this one in succession. As far as I know, it seems like setting the order_with_respect_to takes precedent over any custom ordering. However, now I don't really have a way of getting the data I want without looping through every passage and doing the joins in python. There must be a better way. Maybe i should submit this as a ticket/feature request?

just as a note, i know for a fact that django orders only on the _order attribute regardless of what ordering is set to with this command:


{{{
Question.objects.filter(passage__section=s).values_list('_order',flat=True)
}}}

My desired list here would be a list similar to this:


{{{
[0,1,2,3,4,5,6,7,8,0,1,2,3,4,5,0,1,2,3,4,5,6,7,8......
}}}

but instead i get:


{{{
[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2.......
}}}

a work-around I found was to append 
{{{
order_by('passage___order','_order'
)}} 
to the desired queryset, but this should be a built in ordering feature or at the least be allowed to set not he model-level."	New feature	closed	Database layer (models, ORM)	1.4-beta-1	Normal	wontfix			Design decision needed	0	0	0	0	0	0
