feat: migrate mcq answer from int to list
This commit is contained in:
28
tutorial/migrations/0006_migrate_mcq_answers.py
Normal file
28
tutorial/migrations/0006_migrate_mcq_answers.py
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
def migrate_mcq_answers(apps, schema_editor):
|
||||||
|
Exercise = apps.get_model("tutorial", "Exercise")
|
||||||
|
for ex in Exercise.objects.filter(type="mcq"):
|
||||||
|
if isinstance(ex.data.get("answer"), int):
|
||||||
|
ex.data["answer"] = [ex.data["answer"]]
|
||||||
|
ex.save()
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_migrate_mcq_answers(apps, schema_editor):
|
||||||
|
Exercise = apps.get_model("tutorial", "Exercise")
|
||||||
|
for ex in Exercise.objects.filter(type="mcq"):
|
||||||
|
if isinstance(ex.data.get("answer"), list) and len(ex.data["answer"]) == 1:
|
||||||
|
ex.data["answer"] = ex.data["answer"][0]
|
||||||
|
ex.save()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('tutorial', '0005_alter_exercise_type'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(migrate_mcq_answers, reverse_migrate_mcq_answers),
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user