Skip to content

Including data fails on lists #131

@schtibe

Description

@schtibe

If the inclusion parameter is used in retrieving a list of models, every relation is only considered once (utils.py line 417 removes the relation once)

        try:
            included_resources.remove(field_name)
            new_included_resources = [key.replace('%s.' % field_name, '', 1) for key in included_resources]
            relation_instance_or_manager = getattr(resource_instance, field_name)
            serializer_data = resource.get(field_name)
        except ValueError:
            # Skip fields not in requested included resources
            continue

Example:

Assuming table Person:

pk fruit (fk to Fruit) Person
1 1 John
2 1 Jack
3 2 Jill

and table Fruit:

pk name
1 apple
2 banana

Now let's do GET /api/v1/person?include=fruit which is programmed to return all persons

Gives roughly following result (I wrote this from memory ):

    {
        "data": [{
                "type": "persons",
                "id": 1,
                "attributes": { "name": "John" },
                "relationships": {
                    "fruit": {
                        "data": { "type": "fruit", "id": "1" }
                    }
                }
            },
            {
                "type": "persons",
                "id": 2,
                "attributes": { "name": "Jack" },
                "relationships": {
                    "fruit": {
                        "data": { "type": "fruit", "id": "1" }
                    }
                }
            },
            {
                "type": "persons",
                "id": 3,
                "attributes": { "name": "Jill" },
                "relationships": {
                    "fruit": {
                        "data": { "type": "fruit", "id": "2" }
                    }
                }
            }
        # so far so good, now here it gets interesting
        ],
        "included": [{
            "type": "fruit",
            "id": 1,
            "attributes": {
                "name": "apple"
            }
        }]
        # banana is missing, even though the person 3 references it
    },

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions