【问题标题】:django rest framework , Invalid json listdjango rest框架,无效的json列表
【发布时间】:2020-08-07 00:52:48
【问题描述】:

我正在尝试为我的帖子添加标签。它工作正常,但每当我制作序列化程序类(返回 JSON 数据)时,它都会引发如下所示的错误:

 {
    "tags": [
        "Invalid json list. A tag list submitted in string form must be valid json."
    ]
}

我正在使用django-taggitdjango-taggit-serializer 包。

我在 settings.py 中安装的应用

INSTALLED_APPS = [
   'rest_framework',
   'rest_framework_gis',
   'taggit',
   'taggit_serializer'
]

我的模型.py

class Post(models.Model):

   title= models.CharField(max_length=50)
   description = models.TextField()
   location = models.PointField(srid=4326)
   tags = TaggableManager()

我的序列化器.py

class PostCreateSerializer(TaggitSerializer,GeoFeatureModelSerializer):
    tags = TagListSerializerField()
    class Meta:
        model = Post
        geo_field = 'location' 
        fields = [
            'title','description','tags'
         ]

我的意见.py

class PostCreateApiView(CreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostCreateSerializer
    message = 'you have not account'
    permission_classes = [IsAuthenticated]

    def perform_create(self,serializer):
       serializer.save(user=self.request.user)

我看到了一些类似的问题,但它们对我的情况不起作用。 我将 geodjango 与 PostGIS 数据库一起使用。 我要添加的数据:

{
   "title": "my title",
   "description": "nice desc",
   "city": "city name",
   "tags": tag1 tag2 tag3,
   "location": POINT(-123.0208 44.0464)
 }

【问题讨论】:

  • geo django 不太可能干涉这里。你发布什么数据?
  • 有什么关系,想象它不是 geodjango,有答案吗?
  • 服务器响应中已经给出了答案:发送标签列表,而不是字符串。如果您需要更多帮助,我们需要查看您发送的数据以确定
  • 数据是:{ "title": "my title", "description": "nice desc", "city": "city name", "tags": tag1 tag2 tag3, "location ": POINT(-123.0208 44.0464) }
  • 你看到this post了吗?

标签: django django-rest-framework django-taggit


【解决方案1】:

我猜问题出在发送的数据格式上。 尝试以列表格式发送标签,而不是字符串。 例如: { "title": "my title", "description": "nice desc", "city": "city name", "tags": ["tag1", "tag2", "tag3"], "location": POINT(-123.0208 44.0464) }

查看this 了解有关引发异常的更多信息。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-10-29
  • 1970-01-01
  • 2017-03-19
  • 2014-02-17
  • 2015-07-06
  • 1970-01-01
  • 2015-07-14
  • 2017-08-21
相关资源
最近更新 更多