I'm newbie with python django. And I try to create a Multi choices Test website. I'm a Vietnamese so my questions is in Vietnamese.
I got an issues when I try to insert an object Answers into database.
My error:
django.db.utils.DataError: malformed array literal: "{"A": "C\u01a1m", "B": "ch\u00e1o"}"
LINE 1: ...answer", "level") VALUES ('Hôm nay ăn gì', '', 1, '{"A": "C\...
^
DETAIL: Unexpected array element.
My POST
request:
{
"title": "Hôm năy ăn gì",
"category": 3,
"choices": {
"A": "Cơm",
"B": "cháo"
},
"answer": "A"
}
Here is my code in models.py
:
from django.db import models
from django.contrib.postgres.fields import ArrayField
# Create your models here.
class Category(models.Model):
title = models.TextField(null=False, blank=False)
description = models.TextField(null=False, blank=False)
class Question(models.Model):
title = models.TextField(null=False, blank=False)
description = models.TextField(null=False, blank=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
# choices = ArrayField(ArrayField(models.TextField(blank=True)))
choices = models.JSONField(null=False, blank=False)
answer = models.TextField(null=False,blank=False)
level = models.IntegerField(null=True, blank=True)
In views.py
:
from django.db.models import query
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import generics
from rest_framework import viewsets
from app.models import User, Category, Question
from app.serializers import UserSerializer, QuestionSerializer, CategorySerializer
from django.shortcuts import get_object_or_404
# Create your views here.
class CategoryViewSet(viewsets.ModelViewSet):
serializer_class = CategorySerializer
queryset = Category.objects.all()
class QuestionViewSet(viewsets.ModelViewSet):
serializer_class = QuestionSerializer
queryset = Question.objects.all()
In serializers.py
:
from django.contrib.postgres import fields
from rest_framework import serializers
from app.models import User, Question, Category
class CategorySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Category
fields = ['id', 'title', 'description']
class QuestionSerializer(serializers.ModelSerializer):
# category = CategorySerializer(read_only=False)
class Meta:
model = Question
fields = ['id', 'description', 'category', 'choices', 'answer', 'level']
Any solution for me to insert an Vietnamese JSON Object into database. Or Can I use ArrayField instead of JSONField? If yes, can you give me an example?
Thanks very much.
CodePudding user response:
Looks to me like Question
model choices
field has not been changed from an array
to json
field in the database. So what your are seeing is:
select '{"A": "C\u01a1m", "B": "ch\u00e1o"}'::varchar[];
ERROR: malformed array literal: "{"A": "C\u01a1m", "B": "ch\u00e1o"}"
LINE 1: select '{"A": "C\u01a1m", "B": "ch\u00e1o"}'::varchar[];
instead of:
select '{"A": "C\u01a1m", "B": "ch\u00e1o"}'::jsonb;
jsonb
---------------------------
{"A": "Cơm", "B": "cháo"}
In psql
look at the question table and see what the field actually is?
CodePudding user response:
try this
from django.contrib.postgres.fields import JSONField
class Question(models.Model):
choices = JSONField()