Opened 13 years ago

Closed 12 years ago

#17878 closed New feature (wontfix)

Recursive order_with_respect_to

Reported by: eric@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4-beta-1
Severity: Normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Taylor Mitchell, 13 years ago

Triage Stage: UnreviewedDesign decision needed
Type: BugNew feature

This reads more like a feature request than a bug. The docs don't make any promises about the interaction between order_by and order_with_respect_to so it's difficult to classify as a bug. It does say that ordering is the default, which would seem to jive with what you're seeing. We should perhaps document that setting order_with_respect_to makes ordering get ignored.

comment:2 by Aymeric Augustin, 12 years ago

Resolution: wontfix
Status: newclosed

This feature sounds quite complicated to implement and there isn't much interest.

I suggest making your case on django-developers; a patch (at least a proof of concept) would help.

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