序列化器嵌套关系未按应有的方式工作

Hem*_*mel 2 django django-views django-serializer django-rest-framework

嵌套关系 django 1.11

序列化器:

class PostDetailSerializer(ModelSerializer):
    url = post_detail_url
    user = UserSerializer(read_only=True)
    image = SerializerMethodField()
    html = SerializerMethodField()
    tags = TagSerializer(many=True)
    category = CategorySerializer()
    source = SourceSerializer()

    class Meta:
        model = Post
        fields = [
            'id',
            'url',
            'title',
            'image',
            'slug',
            'content',
            'source',
            'source_link',
            'category',
            'tags',
            'html',
            'publish',
            'timestamp',
            'user',
        ]
Run Code Online (Sandbox Code Playgroud)

回复:

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 3,
    "url": "http://127.0.0.1:8000/api/v1/posts/new-postas/",
    "title": "New Postaas",
    "image": null,
    "slug": "new-postas",
    "content": "asssaasssasa",
    "source": {
    "id": 1,
    "name": "prothom alo",
    "slug": "prothom-alo"
    },
    "source_link": "http://prothom-alo.com/",
    "category": {
        "id": 2,
        "url": "http://127.0.0.1:8000/api/v1/posts/category/news/",
        "name": "news"
    },
    "tags": [
        {
            "id": 1,
            "name": "tech",
            "slug": "tech"
        }
    ],
    "html": "<p>asssaasssasa</p>\n",
    "publish": "2017-08-31",
    "timestamp": "2017-08-31T12:28:28.686538Z",
    "user": {
        "id": "ac32460f-fb7e-4755-9f7e-7c13085ee92b",
        "email": "hello@ihemel.net",
        "first_name": "Hasibul Amin",
        "last_name": "Hemel"
    }
}
Run Code Online (Sandbox Code Playgroud)

这是检索数据的精细嵌套关系。

但再次在我的类别详细信息 api 序列化器下面:

class CategoryDetailSerializer(ModelSerializer):
    url = category_detail_url
    posts = PostDetailSerializer(many=True, read_only=True)

    class Meta:
        model = Category
        fields = [
            'id',
            'url',
            'name',
            'posts'
        ]
Run Code Online (Sandbox Code Playgroud)

这里我的 post 序列化器不会在 api 中输出任何数据。我不知道。没有错误或错误,只是值没有到来。

类别详细信息 api 响应:

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 2,
    "url": "http://127.0.0.1:8000/api/v1/posts/category/news/",
    "name": "news"
}
Run Code Online (Sandbox Code Playgroud)

这是图像

有什么解决办法吗?我搜索过但没有找到。

nev*_*ner 6

由于您使用的是字段名posts,因此CategoryDetailSerializer需要在模型related_name=posts内设置类别关系Post

class Post(Model):
    category = ForeignKey(Category, related_name='posts')
Run Code Online (Sandbox Code Playgroud)

post_set或者您可以在以下位置使用默认关系名称CategoryDetailSerializer

class CategoryDetailSerializer(ModelSerializer):
    url = category_detail_url
    post_set = PostDetailSerializer(many=True, read_only=True)

    class Meta:
        model = Category
        fields = [
        'id',
        'url',
        'name',
        'post_set'
        ]
Run Code Online (Sandbox Code Playgroud)

请参阅此处的详细信息。

您还可以尝试使用模型中的 related_name 在序列化器字段上指定源:

posts = PostDetailSerializer(many=True, read_only=True, source='cat_posts')
Run Code Online (Sandbox Code Playgroud)