'Update Boolean field and Integer fields from Django view
Can someone please point me in the right direction here. I have a quiz app where I want to implement some user progress objects. I have a QuizResult model which foreignkeys to user, quiz and lesson. What I want is to be able to update a couple of fields: score (integer field) and complete (boolean field) from my views.py function. when I try to update these fields from my frontend I get a no errors, but the object shows complete and score fields as null.
Here is my model:
class QuizResult(models.Model):
student = models.ForeignKey(User, related_name="results", on_delete=models.CASCADE)
quiz = models.ForeignKey(Quiz, related_name="results", on_delete=models.CASCADE)
lesson = models.ForeignKey(Lesson, null=True, blank=True, related_name="results", on_delete=models.CASCADE)
score = models.IntegerField(blank=True, null=True)
complete = models.BooleanField(default=False)
and my view.py function:
@api_view(['POST'])
def post_progress(request, pk):
data = request.data
score = data.get('score')
complete = data.get('complete')
quiz = Quiz.objects.get(pk=pk)
user_progress = QuizResult.objects.create(quiz=quiz, score=score, complete=complete, student=request.user)
serializer = QuizResultSerializer(user_progress, data=data)
return Response(serializer.data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
