Skip to content

Use QuestionAssignment.order where we expect it#2743

Merged
niklasmohrin merged 2 commits into
e-valuation:mainfrom
niklasmohrin:question-ordering
Jun 29, 2026
Merged

Use QuestionAssignment.order where we expect it#2743
niklasmohrin merged 2 commits into
e-valuation:mainfrom
niklasmohrin:question-ordering

Conversation

@niklasmohrin

Copy link
Copy Markdown
Member

Closes #2741

When we introduced QuestionAssignment we moved the order field from Question to QuestionAssignment. Semantically, this makes sense, because the same question might be ordered differently in different questionnaires. However, specifying an ordering on a through-model like this does not result in Django ordering the related instances as we expect (see https://code.djangoproject.com/ticket/30460). Instead, the order of the related model is used, which in our case (on Question) was unspecified, so the database just returned some arbitrary order.

The fix is to not iterate over questionnaire.questions and instead prefer going via questionnaire.question_assignments. This ensures that the QuestionAssignment ordering is used. Another option would be to always prefetch with custom Prefetch objects like Prefetch("questions", queryset=Question.objects.order_by("assignments")). However, this seems less robust then explicitly going via assignments in the right places and also uses the order_by("through-model") hack from the Django ticket which, as far as I can tell, is not documented.

The places from the issue seem to be fixed by this PR. I checked for other places by searching for the strings .questions and questions".

Comment thread evap/student/forms.py

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to have tests that check this ordering? I think that we decided in previous cases that ordering is something that is usually just right if we have the correct Meta.ordering set, but I feel that this is not the case here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is a bit more tricky here, and we should add a test. The wrong order of questions can make a questionnaire almost unusable.

@janno42 janno42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔧

@richardebeling richardebeling left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes here lgtm, although I can definitely see that people will stumble over this again in the future. Let's discuss options in person some time (I'm thinking of asking django to respect M2M ordering, I feel like this is a reasonable ask, and maybe building some workaround in the meantime)

@niklasmohrin niklasmohrin marked this pull request as ready for review June 21, 2026 21:07
@niklasmohrin niklasmohrin merged commit 600eb45 into e-valuation:main Jun 29, 2026
15 of 16 checks passed
@niklasmohrin niklasmohrin deleted the question-ordering branch June 29, 2026 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect Question Order

3 participants