Skip to content

Commit

Permalink
Fix recursive extras.
Browse files Browse the repository at this point in the history
  • Loading branch information
iurisilvio committed May 15, 2021
1 parent 6043eb1 commit bbe48b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion django_qserializer/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def prepare_objects(self, objs):
def _serialize_object(self, obj):
serialized = self.serialize_object(obj)
for extra in self.extra.values():
serialized.update(extra.serialize_object(obj))
serialized.update(extra._serialize_object(obj))
return serialized

def serialize_object(self, obj):
Expand Down
42 changes: 42 additions & 0 deletions django_qserializer/tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,45 @@ def serialize_object(self, obj):

with django_assert_num_queries(0):
assert expected == next(serialize([bus]))


def test_extras_recursive(bus_fixture, db, django_assert_num_queries):
def city(obj):
return {
'city': 'SJK',
}

class Attr(BaseSerializer):
extra = {
'city': city,
}
select_related = ['company']

def serialize_object(self, obj):
return {
'myattr': obj.company.name
}

class S(BaseSerializer):
extra = {
'myattr': Attr(extra=['city']),
}

def serialize_object(self, obj):
return {
'plate': obj.plate,
}

serializer = S(extra=['myattr'])

with django_assert_num_queries(1):
bus = Bus.objects.to_serialize(serializer).first()

expected = {
'plate': 'BUSER',
'myattr': 'Hurricane Cart',
'city': 'SJK',
}

with django_assert_num_queries(0):
assert expected == bus.serialize()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = django_qserializer
version = 0.2.3
version = 0.2.4
url = https://github.com/buserbrasil/django-qserializer
license = MIT
author = Iuri de Silvio
Expand Down

0 comments on commit bbe48b4

Please sign in to comment.