简体   繁体   English

Django Rest 如何显示所有相关外键object?

[英]Django Rest How to show all related foreignkey object?

I have an blog website and my visitors can also comment on my blog posts.我有一个博客网站,访问者也可以对我的博客文章发表评论。 Each blog post have multiple comment and I want to show those comment under my each single blog post .每篇博文都有多条评论,我想在每篇文下显示这些评论。 Assume Blog1 have 10 comment so all 10 comment will be show under Blog1假设Blog1有 10 条评论,那么所有 10 条评论都将显示在Blog1

here is my code:这是我的代码:

models.py模型.py

class Blog(models.Model):
    blog_title = models.CharField(max_length=200, unique=True)

class Comment(models.Model):
  name = models.CharField(max_length=100)
  email = models.EmailField(max_length=100)
  comment = models.TextField()
  blog = models.ForeignKey(Blog, on_delete=models.CASCADE)

Serializer.py序列化器.py

class CommentSerializer(serializers.ModelSerializer):
      
      class Meta:
          model = Comment
          fields = '__all__' 


class BlogSerializer(serializers.ModelSerializer):  
    class Meta:
        model = Blog
        exclude = ("author", "blog_is_published")
        lookup_field = 'blog_slug'
        extra_kwargs = {
            'url': {'lookup_field': 'blog_slug'}
        }

views.py:意见.py:

class BlogViewSet(viewsets.ModelViewSet):
    queryset = Blog.objects.all().order_by('-id')
    serializer_class = BlogSerializer
    pagination_class = BlogPagination
    lookup_field = 'blog_slug'

You can access comments list from blog object using comment_set attribute, so add comment_set field to your serializer:您可以使用comment_set属性从博客 object 访问评论列表,因此将comment_set字段添加到您的序列化程序:

class BlogSerializer(serializers.ModelSerializer):  
    comment_set = CommentSerializer(many=True)

    class Meta:
        model = Blog
        exclude = ("author", "blog_is_published")
        lookup_field = 'blog_slug'
        extra_kwargs = {
            'url': {'lookup_field': 'blog_slug'}
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:过滤与ForeignKey对象相关的所有操作 - Django: Filter for all actions related to the ForeignKey Object Django Rest框架,如何使用foreignkey_id字段包含“ __all__”字段和相关字段 - Django Rest framework, how to include '__all__' fields and a related field using foreignkey_id field 如何在Django模型ForeignKey和OneToOneField中获取所有相关对象 - How to get all related objects in django model ForeignKey and OneToOneField 如何在Django Rest框架中获取外键对象以显示完整对象 - how to get a foreignkey object to display full object in django rest framework 如何使用'select_related'接收来自related(ForeignKey)Django模型的所有字段 - How to recieve not all fields from related(ForeignKey) django model using 'select_related' Django:如何获取与列表相关的ForeignKey记录? - Django: how to get list related ForeignKey records? Django REST-元数据中与ForeignKey相关的模型的URL(OPTIONS) - Django REST - URL of ForeignKey related model in metadata(OPTIONS) django - 模型unicode()显示foreignkey对象属性 - django - model unicode() show foreignkey object attribute 在Django中获取与此模型相关的所有模型(通过ForeignKey,ManyToMany) - Get all models related to this model in django (via ForeignKey, ManyToMany) 内联编辑与模型相关的所有ForeignKey [django admin] - inline edit all ForeignKey related to a model[django admin]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM