Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中创建与单个blogpost细节相关的评论帖子api?_Python_Django_Api_Comments_Django Serializer - Fatal编程技术网

Python 如何在django中创建与单个blogpost细节相关的评论帖子api?

Python 如何在django中创建与单个blogpost细节相关的评论帖子api?,python,django,api,comments,django-serializer,Python,Django,Api,Comments,Django Serializer,我有一个显示博客文章详细信息的页面。在帖子下方有一个部分,用户可以在评论框中输入自己的姓名、主题和文本后发表评论。现在我必须为此制作一个api。我想制作一个postapi,以便将注释存储/关联到特定的blogpost细节。这意味着我需要在发布评论时传递blogpost id。怎么做 class BlogPost(models.Model): CATEGORY_CHOICES = ( ('travel_news', 'Travel News',), ('tr

我有一个显示博客文章详细信息的页面。在帖子下方有一个部分,用户可以在评论框中输入自己的姓名、主题和文本后发表评论。现在我必须为此制作一个api。我想制作一个postapi,以便将注释存储/关联到特定的blogpost细节。这意味着我需要在发布评论时传递blogpost id。怎么做

class BlogPost(models.Model):
    CATEGORY_CHOICES = (
        ('travel_news', 'Travel News',),
        ('travel_tips', 'Travel Tips',),
        ('things_to_do', 'Things to Do',),
        ('places_to_go', 'Places to Go'),
    )
    image = models.ImageField(blank=True, null=True)
    categories = models.CharField(max_length=64, choices=CATEGORY_CHOICES, default='travel_news')
    description = models.CharField(max_length=255)
    content = RichTextUploadingField()
   
    # todo support for tags
    tags = models.CharField(max_length=255, default='#travel') #todo
    date_created = models.DateField()

    @property
    def html_stripped(self):
       from django.utils.html import strip_tags
       return strip_tags(self.content)
         
    @property
    def comments(self):
        return self.comments_set.all()
以下是我的序列化程序:

class CommentPostSerializer(serializers.ModelSerializer):

    class Meta:
        model = Comment
        # fields = '__all__'
        fields = ['name', 'email', 'subject', 'comment',]



class BlogPostSerializer(serializers.ModelSerializer):
    comments = CommentListSerializer(many=True)
    class Meta:
        model = BlogPost
        fields = ['image', 'categories', 'description', 'content', 'tags', 'date_created', 'comments']
        # fields = '__all__'
以下是我的看法:

class CommentCreateAPIView(CreateAPIView):
    queryset = Comment.objects.all()
    serializer_class = CommentPostSerializer

“这意味着我需要在发布评论时通过blogpost id”。。。将相关数据作为post有效负载传递。除此之外,
APIView
class对放置
queryset
serializer\u class
AttributesOrry(其CreateAPIView在我的代码中)没有任何影响。我在这里发帖时犯了一个错误。我在这里编辑过。你能检查一下吗。好的,现在我应该向我的序列化程序/视图添加什么,以便我的特定评论只与该博客文章关联,而不是与其他博客文章关联??我是新来的。你能陪我走过吗??