序列化程序嵌套关系无法正常工作

时间:2017-08-31 18:16:26

标签: django django-rest-framework django-views django-serializer

嵌套关系 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',
        ]

响应:

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"
    }
}

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

但在我的类别中再次详细介绍api序列化器:

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

    class Meta:
        model = Category
        fields = [
            'id',
            'url',
            'name',
            'posts'
        ]

这里我的帖子序列化程序不会在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"
}

Here the Image

有什么解决方案吗?我搜索但没找到任何。

2 个答案:

答案 0 :(得分:1)

由于您在posts中使用了字段名CategoryDetailSerializer,因此需要在related_name=posts模型中将Post设置为类别关系:

class Post(Model):
    category = ForeignKey(Category, related_name='posts')

或者您可以在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'
        ]

详见here

您还可以尝试使用model中的related_name在序列化程序字段中指定source:

posts = PostDetailSerializer(many=True, read_only=True, source='cat_posts')

答案 1 :(得分:0)

我认为没有调用CategoryDe​​tailSerializer(), 所谓的是其他类别序列化程序,它是CategorySerializer()。

相关问题