Questions tagged «django-serializer»

7
Django rest框架序列化多对多字段
如何将多对多字段序列化为列表,然后通过rest框架返回它们?在下面的示例中,我尝试返回该帖子以及与其关联的标签列表。 models.py class post(models.Model): tag = models.ManyToManyField(Tag) text = models.CharField(max_length=100) serializers.py class PostSerializer(serializers.ModelSerializer): class Meta: model = Post fields = ("text", "tag"??) views.py class PostViewSet(viewsets.ReadOnlyModelViewSet): queryset = Post.objects.all() serializer_class = PostSerializer
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.