Trying to return DB Array object in Django to a View

I have something sort of working. I am very new to Django and Python. I have the following view in my api folder

from re import S
from django.shortcuts import render
from rest_framework import generics, status
from .serializers import CourseSerializer, CreateCourseSerializer
from .models import Course
from rest_framework.views import APIView
from rest_framework.response import Response
import logging
from django.core import serializers


logging.basicConfig(level=logging.WARNING)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)


# Create your views here.


class CourseView(generics.ListAPIView):
    queryset = Course.objects.all()
    serializer_class = CourseSerializer

    SomeModel_json = serializers.serialize("json", Course.objects.all())

    logger.debug(SomeModel_json)

    def get(self, request, format=None):

        return Response(self.SomeModel_json, status=status.HTTP_201_CREATED)


class CreateCourseView(APIView):
    serializer_class = CreateCourseSerializer

    def post(self, request, format=None):
        if not self.request.session.exists(self.request.session.session_key):
            self.request.session.create()

        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            course_title = serializer.data.get('course_title')
            course_topic = serializer.data.get('course_topic')
            course_topic_about = serializer.data.get('course_topic_about')
            course_question = serializer.data.get('course_question')
            course_answer = serializer.data.get('course_answer')

            course = Course(course_title=course_title, course_topic=course_topic,
                            course_topic_about=course_topic_about, course_question=course_question, course_answer=course_answer)
            course.save()

            return Response(CourseSerializer(course).data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

and have the following serializers.py in my api folder

from rest_framework import serializers
from .models import Course


class CourseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Course
        fields = (
            'id', 'course_title', 'course_topic', 'course_topic_about', 'course_question', 'course_answer', 'created_at', 'updated_at')


class CreateCourseSerializer(serializers.ModelSerializer):
    class Meta:
        model = Course
        fields = (
            'course_title', 'course_topic', 'course_topic_about', 'course_question', 'course_answer')

Have the follow models.py in api folder

from django.db import models

# Create your models here.


class Course(models.Model):
    course_title = models.CharField(max_length=255)
    course_topic = models.TextField(max_length=255)
    course_topic_about = models.TextField(max_length=255)
    course_question = models.TextField(max_length=255)
    course_answer = models.TextField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

This is my get view

get view

This is my post view

post view

Now what I don't understand is why the get view is stringified like so

"[{\"model\": \"api.course\", \"pk\": 1, \"fields\": {\"course_title\": \"Understanding the weird parts of javascript\", \"course_topic\": \"Javascript Objects\", \"course_topic_about\": \"Everything in JavaScript acts like an object\", \"course_question\": \"In JavaScript what is not an object?\", \"course_answer\": \"Null and Undefined\", \"created_at\": \"2021-12-09T03:16:37.667Z\", \"updated_at\": \"2021-12-09T03:16:37.667Z\"}}, {\"model\": \"api.course\", \"pk\": 2, \"fields\": {\"course_title\": \"Understanding the weird parts of javascript\", \"course_topic\": \"test\", \"course_topic_about\": \"test\", \"course_question\": \"test\", \"course_answer\": \"test\", \"created_at\": \"2021-12-11T00:20:08.233Z\", \"updated_at\": \"2021-12-11T00:20:08.233Z\"}}, {\"model\": \"api.course\", \"pk\": 3, \"fields\": {\"course_title\": \"test\", \"course_topic\": \"test\", \"course_topic_about\": \"test\", \"course_question\": \"test\", \"course_answer\": \"test\", \"created_at\": \"2021-12-11T02:02:19.997Z\", \"updated_at\": \"2021-12-11T02:02:19.997Z\"}}]"

Is there a way to serialize the object so it's not stringified. I am using React in my front end and I guess I could just parse the string.

But I have a feeling I am doing something wrong in Django python side to end up with this result, since my post request is not stringified when I get the result. I didn't write the post view myself I followed a tutorial so don't understand each piece just yet.

Any help would be great

Thanks ahead of time

Back to Top