如何将字符可变数组插入PostgreSQL

时间:2018-11-25 15:40:29

标签: django postgresql

我想在PostgreSQL中插入一个json数组吗?这是我的代码:

def post(self,request):
    request_data = '''
      {
        "name":"a",
        "isbn":"dd",
        "author":["a","b"],
        "publisher":["a","b"],
        "price":"ddd"}
    '''
    data = json.loads(request_data)
    serializer = BookSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
        return JsonResponse(serializer.data, status=201)
    return JsonResponse(serializer.errors, status=400)

这是我的字段定义:

class BookSerializer(serializers.Serializer):                
    name = serializers.CharField(required=False, allow_blank=True, max_length=100)
    isbn = serializers.CharField(required=False, allow_blank=True, max_length=100)
    author = serializers.ListField(required=False)
    publisher = serializers.ListField(required=False, max_length=100)
    price = serializers.CharField(required=False, allow_blank=True, max_length=100)

模型中的书定义为:

class Book(models.Model):
    name = models.CharField(max_length=256, blank=True, default='')
    isbn = models.CharField(max_length=16,blank=False)
    author = models.CharField(max_length=16,blank=False)
    publisher = models.CharField(max_length=16,blank=False)
    price = models.CharField(max_length=16,blank=False)

    class Meta:
        db_table = 'book'

当我请求该函数时,错误堆栈输出如下:

 malformed array literal: "['a', 'b']"
LINE 1: ...author", "publisher", "price") VALUES ('a', 'dd', '[''a'', '...
                                                             ^
DETAIL:  "[" must introduce explicitly-specified array dimensions.
Request Method: GET
Request URL:    http://localhost:8000/spider/api/book
Django Version: 2.1.3
Exception Type: DataError
Exception Value:    
malformed array literal: "['a', 'b']"
LINE 1: ...author", "publisher", "price") VALUES ('a', 'dd', '[''a'', '...
                                                             ^
DETAIL:  "[" must introduce explicitly-specified array dimensions.
Exception Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py in _execute, line 85
Python Executable:  /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Python Version: 3.7.1
Python Path:    
['/Users/dolphin/source/pydolphin',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
 '/Users/dolphin/Library/Python/3.7/lib/python/site-packages',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']
Server time:    Sun, 25 Nov 2018 15:42:19 +0000

如何避免这个问题?

1 个答案:

答案 0 :(得分:0)

只需调整数组模型定义,如下所示:

class Book(models.Model):
    name = models.CharField(max_length=256, blank=True, default='')
    isbn = models.CharField(max_length=16,blank=False)
    author = ArrayField(models.CharField(max_length=200))
    publisher = ArrayField(models.CharField(max_length=16,blank=False))
    price = models.CharField(max_length=16,blank=False)

别忘了导入参考:

from django.contrib.postgres.fields import ArrayField
from django.db.models import FileField