def save(self, user, id=None): cd = self.cleaned_data keypoint = cd['keypoint'].split(',') if id: with transaction.atomic(): choiceProblem = ChoiceProblem.objects.select_for_update().get(pk=id) choiceProblem.c = cd['c'] choiceProblem.title = cd['title'] choiceProblem.a = cd['a'] choiceProblem.b = cd['b'] choiceProblem.d = cd['d'] choiceProblem.right_answer = cd['selection'] #choiceProblem.creater = user choiceProblem.classname.clear() choiceProblem.knowledgePoint1.clear() choiceProblem.save() else: choiceProblem = ChoiceProblem(title=cd['title'], a=cd['a'], b=cd['b'], c=cd['c'], d=cd['d'], right_answer=cd['selection'], creater=user) choiceProblem.save() for point in keypoint: choiceProblem.knowledgePoint2.add(KnowledgePoint2.objects.get(pk=point)) for point in choiceProblem.knowledgePoint2.all(): choiceProblem.knowledgePoint1.add(point.upperPoint) for point in choiceProblem.knowledgePoint1.all(): choiceProblem.classname.add(point.classname) choiceProblem.save() return choiceProblem
def save(self, user, id=None): cd = self.cleaned_data keypoint = cd['keypoint'].split(',') if id: with transaction.atomic(): choiceProblem = ChoiceProblem.objects.select_for_update().get( pk=id) choiceProblem.c = cd['c'] choiceProblem.title = cd['title'] choiceProblem.a = cd['a'] choiceProblem.b = cd['b'] choiceProblem.d = cd['d'] choiceProblem.right_answer = cd['selection'] choiceProblem.creater = user choiceProblem.classname.clear() choiceProblem.knowledgePoint1.clear() choiceProblem.save() else: choiceProblem = ChoiceProblem(title=cd['title'], a=cd['a'], b=cd['b'], c=cd['c'], d=cd['d'], right_answer=cd['selection'], creater=user) choiceProblem.save() for point in keypoint: choiceProblem.knowledgePoint2.add( KnowledgePoint2.objects.get(pk=point)) for point in choiceProblem.knowledgePoint2.all(): choiceProblem.knowledgePoint1.add(point.upperPoint) for point in choiceProblem.knowledgePoint1.all(): choiceProblem.classname.add(point.classname) choiceProblem.save() return choiceProblem