enter image description here
enter image description here
my system can show question have save in the table, but just random way. create code that categorizes questions by difficulty level (easy, medium, hard), and displays them in ascending order of difficulty.
1
Option 1
- Change your
Questionbank.level
field to be an IntegerField (with choices e.g.[(10, 'easy'), (20, 'medium'), (30, 'hard')]
) (if you already have question data, this will require a migration) Questionbank.filter(assignment=assignment).order_by('level')
Option 2
or if you absolutely can’t change level
to be numeric, use case/when to map it during the selection, but this is an objectively worse solution:
Questionbank.filter(assignment=assignment).annotate(
level_num=Case(
When(level="easy", then=Value(1)),
When(level="medium", then=Value(2)),
When(level="hard", then=Value(3)),
default=Value(0),
),
).order_by("level_num")
0