From feb48711be226998a1a1ff2ad7a09e70787f19f2 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Thu, 27 Apr 2023 14:59:50 +0200 Subject: [PATCH] add "id" as an optional value of the serializer fields returning the haystack internal id --- drf_haystack/serializers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drf_haystack/serializers.py b/drf_haystack/serializers.py index a77c885..3bca093 100644 --- a/drf_haystack/serializers.py +++ b/drf_haystack/serializers.py @@ -211,6 +211,10 @@ def get_fields(self): if declared_fields: for field_name in declared_fields: field_mapping[field_name] = declared_fields[field_name] + + if "id" in fields: + field_mapping['id'] = HaystackCharField() + return field_mapping def to_representation(self, instance): @@ -236,7 +240,7 @@ def to_representation(self, instance): if index == current_index: ret[field] = ret[orig_field] del ret[orig_field] - elif field not in chain(instance.searchindex.fields.keys(), self._declared_fields.keys()): + elif field not in chain(instance.searchindex.fields.keys(), self._declared_fields.keys(), ["id"]): del ret[orig_field] # include the highlighted field in either case