'DRF return model instances not saved in database

I have a Assignment model -

class Assignment(models.Model):
    classroom = models.ForeignKey(Classroom, on_delete=models.CASCADE)
    title = models.CharField(max_length=500)
    text = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    due_date_time = models.DateTimeField()

and a Submission model -

class Submission(models.Model):
    student = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
    assignment = models.ForeignKey(Assignment, on_delete=models.CASCADE)
    text = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    @property
    def status(self):
        if self.created_at <= self.assignment.due_date_time:
            return 'Done'
        return 'Submitted Late'

A Teacher should be able to get all submissions for an assignment But, if a Student doesn't submit anything, there won't be a Submission for that particular student.

This is the view for getting submissions -

def submissions(request, code, assignment_id):
    classroom = get_object_or_404(Classroom, code=code)
    assignment = get_object_or_404(Assignment, id=assignment_id)

    if assignment.classroom != classroom:
        return Response(status=status.HTTP_404_NOT_FOUND)

    user = request.user
    if request.method == 'GET':
        if user != classroom.teacher:
            return Response(status=status.HTTP_403_FORBIDDEN)

        data = []
        for student in classroom.students.all():
            submission = get_user_submission(assignment, student)
            serializer = get_submission_data(assignment.due_date_time, student, submission)
            data.append(serializer.data)

        return Response(data)


def get_user_submission(assignment, user):
    all_submissions = assignment.submission_set.all()
    for submission in all_submissions:
        if submission.student == user:
            return submission
    return None


def get_submission_data(due_date_time, student, submission):
    if submission is not None:
        return TeacherSubmissionSerializer({'student': student, 'submission': submission, 'status': submission.status})

    if due_date_time > datetime.now(timezone.utc):
        return TeacherSubmissionSerializer({'student': student, 'submission': submission, 'status': 'Assigned'})
    return TeacherSubmissionSerializer({'student': student, 'submission': submission, 'status': 'Missing'})

This is the TeacherSubmissionSerializer -

class TeacherSubmissionSerializer(serializers.Serializer):
    student = UserSerializer()
    submission = SubmissionSerializer()
    status = serializers.CharField()


class SubmissionSerializer(serializers.ModelSerializer):
    student = UserSerializer(read_only=True)

    class Meta:
        model = Submission
        fields = ('text', 'created_at')

What I want to do is return a list of students along with their submissions and status.
If there is no submission, how should I handle that case?
Currently, I return submission as null, but is there a better way to solve it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source