From c428efded0b22bc663dec0a349ce8bd4bfb3c540 Mon Sep 17 00:00:00 2001 From: Pauliina Ilmanen Date: Thu, 8 Aug 2024 15:09:08 +0300 Subject: [PATCH] Search: handle department & municipality serialization in unit results The issue was noticed when using `include=unit.department` in search raised "Object of type Department is not JSON serializable" errors. Add department and municipality serialization to the search results to fix the issue. --- services/search/api.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/services/search/api.py b/services/search/api.py index ac858a1dd..6dfd8cd9e 100644 --- a/services/search/api.py +++ b/services/search/api.py @@ -193,6 +193,12 @@ def to_representation(self, obj): representation["connections"] = UnitConnectionSerializer( obj.connections, many=True ).data + elif "department" in include_field: + representation["department"] = DepartmentSerializer( + obj.department + ).data + elif "municipality" in include_field: + representation["municipality"] = obj.municipality.id else: if hasattr(obj, include_field): representation[include_field] = getattr(