Skip to content

Commit

Permalink
Merge branch 'release/0.6.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlov99 committed Feb 26, 2015
2 parents e3f431b + f8b8d90 commit 17547bf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion jsonapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" JSON:API realization."""
__version = (0, 6, 11)
__version = (0, 6, 12)

__version__ = version = '.'.join(map(str, __version))
__project__ = PROJECT = __name__
22 changes: 15 additions & 7 deletions jsonapi/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,21 @@ def dump_documents(cls, resource, model_instances, fields_own=None,
field.related_model]
related_model_info = related_resource.Meta.model_info

data["linked"][related_resource.Meta.name_plural] = [
related_resource.dump_document(
getattr(m, field.name),
[f.name for f in related_model_info.fields_own]
) for m in model_instances
if getattr(m, field.name) is not None
]
# NOTE: could not use select+distinct because objects are prefetched
# from the database and queryset is evaluated only once.
linked_ids = set()
linked_objs = []
for m in model_instances:
rel_model = getattr(m, field.name)
if rel_model is not None and rel_model.id not in linked_ids:
linked_objs.append(related_resource.dump_document(
rel_model,
[f.name for f in related_model_info.fields_own]
))
linked_ids.add(rel_model.id)

if linked_objs:
data["linked"][related_resource.Meta.name_plural] = linked_objs

for field in fields_to_many:
related_resource = cls.Meta.api.model_resource_map[
Expand Down
7 changes: 4 additions & 3 deletions tests/testapp/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ def test_get_top_level_links(self):
self.assertEqual(data, expected_data)

def test_get_include(self):
post = mixer.blend("testapp.post")
author = mixer.blend("testapp.author")
mixer.cycle(2).blend("testapp.post", author=author)
response = self.client.get(
'/api/post?include=author',
content_type='application/vnd.api+json',
Expand All @@ -471,8 +472,8 @@ def test_get_include(self):
data = json.loads(response.content.decode("utf-8"))["linked"]
expected_data = {
"authors": [{
"id": post.author_id,
"name": post.author.name
"id": author.id,
"name": author.name
}]
}
self.assertEqual(data, expected_data)
Expand Down

0 comments on commit 17547bf

Please sign in to comment.