|Reported by:||eric@…||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.4-beta-1|
|Cc:||Triage Stage:||Design decision needed|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
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)
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)
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:
My desired list here would be a list similar to this:
but instead i get:
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 Changed 3 years ago by tmitchell
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Design decision needed
- Type changed from Bug to New feature