【发布时间】:2020-12-25 11:55:49
【问题描述】:
我正在使用 DRF 设置 API,一切进展顺利,但在传递带有字符串列表的字段时遇到了一点问题
Json 对象
{
"postID": 1,
"index": 0,
"text": "For years you had a President who apologized for America – now you have a President who is standing up for America, and standing up for PENNSYLVANIA. Tomorrow, you have the power, with your vote, to save AMERICA! GET OUT AND VOTE!! #MAGA ",
"date": "2020-11-02",
"likesCount": 145000,
"commentsCount": 4500,
"sharesCount": 3500,
"hashtags": [
"[",
"'",
"M",
"A",
"G",
"A",
"'",
" ",
"'"
"A",
"G",
"A",
"I",
"N",
"'",
"]"
]
}
而 hashtags 字段值应为:“['MAGA' , 'AGAIN']”。
如何覆盖序列化程序并防止字符串被拆分为字符?
models.py
from djongo import models
from django.contrib.postgres.fields import ArrayField
# Create your models here.
class Post(models.Model):
index = models.IntegerField()
postID = models.IntegerField(primary_key=True)
text = models.CharField(max_length=500)
date = models.DateField()
likesCount = models.IntegerField()
commentsCount = models.IntegerField()
sharesCount = models.IntegerField()
hashtags = ArrayField(models.CharField(max_length=20, blank=True), size=50)
tokens = ArrayField(models.CharField(max_length=20, blank=True), size=50)
tagged = ArrayField(models.CharField(max_length=20, blank=True), size=50)
keywords = ArrayField(models.CharField(max_length=20, blank=True), size=50)
entities = ArrayField(models.CharField(max_length=20, blank=True), size=50)
noun_phrases = ArrayField(models.CharField(max_length=20, blank=True), size=50)
noun_phrases_keywords = ArrayField(models.CharField(max_length=20, blank=True), size=50)
serializer.py
from rest_framework import serializers
from .models import Post, Comments
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = '__all__'
序列化程序检查
PostSerializer():
postID = IntegerField(label='PostID', max_value=2147483647, min_value=-2147483648, validators=[<UniqueValidator(queryset=Post.objects.all())>])
index = IntegerField(max_value=2147483647, min_value=-2147483648)
text = CharField(max_length=500)
date = DateField()
likesCount = IntegerField(label='LikesCount', max_value=2147483647, min_value=-2147483648)
commentsCount = IntegerField(label='CommentsCount', max_value=2147483647, min_value=-2147483648)
sharesCount = IntegerField(label='SharesCount', max_value=2147483647, min_value=-2147483648)
hashtags = ListField(allow_empty=False, child=CharField(allow_blank=True, label='Hashtags', max_length=20, required=False), validators=[<django.contrib.postgres.validators.ArrayMaxLengthValidator object>])
tokens = ListField(allow_empty=False, child=CharField(allow_blank=True, label='Tokens', max_length=20, required=False), validators=[<django.contrib.postgres.validators.ArrayMaxLengthValidator object>])
tagged = ListField(allow_empty=False, child=CharField(allow_blank=True, label='Tagged', max_length=20, required=False), validators=[<django.contrib.postgres.validators.ArrayMaxLengthValidator object>])
keywords = ListField(allow_empty=False, child=CharField(allow_blank=True, label='Keywords', max_length=20, required=False), validators=[<django.contrib.postgres.validators.ArrayMaxLengthValidator object>])
entities = ListField(allow_empty=False, child=CharField(allow_blank=True, label='Entities', max_length=20, required=False), validators=[<django.contrib.postgres.validators.ArrayMaxLengthValidator object>])
noun_phrases = ListField(allow_empty=False, child=CharField(allow_blank=True, label='Noun phrases', max_length=20, required=False), validators=[<django.contrib.postgres.validators.ArrayMaxLengthValidator object>])
noun_phrases_keywords = ListField(allow_empty=False, child=CharField(allow_blank=True, label='Noun phrases keywords', max_length=20, required=False), validators=[<django.contrib.postgres.validators.ArrayMaxLengthValidator object>])
【问题讨论】: