SOITA/trials/managers.py

43 lines
1.2 KiB
Python
Raw Normal View History

2021-12-12 11:35:35 +01:00
from django.db.models import Manager
from django.apps import apps
from django.conf import settings
from categories.models import Category
class TestManager(Manager):
def create(
self, *,
category=None,
questions=[],
**kwargs
):
Question = apps.get_model("questions", "Question")
Answer = apps.get_model("answers", "Answer")
category = Category.objects.get_or_create(
name=category["name"]
)
instance = super().create(
name=kwargs.get("name"),
passing_score=kwargs.get("passing_score"),
category=category[0]
)
2021-12-12 23:34:54 +01:00
# import pdb;pdb.set_trace()
2021-12-12 11:35:35 +01:00
for question in questions:
question_instance = Question.objects.create(
name=question["name"],
2021-12-12 23:34:54 +01:00
description=question["description"],
test=instance
2021-12-12 11:35:35 +01:00
)
for answer in question["answers"]:
2021-12-12 23:34:54 +01:00
import pdb;
pdb.set_trace()
2021-12-12 11:35:35 +01:00
Answer.objects.create(
description=answer["description"],
is_correct=answer["is_correct"],
question=question_instance
)
return instance