from rest_framework import fields
[docs]
class DRFHaystackFieldMixin:
prefix_field_names = False
def __init__(self, **kwargs):
self.prefix_field_names = kwargs.pop("prefix_field_names", False)
super().__init__(**kwargs)
[docs]
def bind(self, field_name, parent):
"""
Initializes the field name and parent for the field instance.
Called when a field is added to the parent serializer instance.
Taken from DRF and modified to support drf_haystack multiple index
functionality.
"""
# In order to enforce a consistent style, we error if a redundant
# 'source' argument has been used. For example:
# my_field = serializer.CharField(source='my_field')
assert self.source != field_name, (
f"It is redundant to specify `source='{field_name}'` on field '{self.__class__.__name__}' in "
f"serializer '{parent.__class__.__name__}', because it is the same as the field name. "
"Remove the `source` keyword argument."
)
self.field_name = field_name
self.parent = parent
# `self.label` should default to being based on the field name.
if self.label is None:
self.label = field_name.replace("_", " ").capitalize()
# self.source should default to being the same as the field name.
if self.source is None:
self.source = self.convert_field_name(field_name)
# self.source_attrs is a list of attributes that need to be looked up
# when serializing the instance, or populating the validated data.
if self.source == "*":
self.source_attrs = []
else:
self.source_attrs = self.source.split(".")
[docs]
def convert_field_name(self, field_name):
if not self.prefix_field_names:
return field_name
return field_name.split("__")[-1]
[docs]
class HaystackBooleanField(DRFHaystackFieldMixin, fields.BooleanField):
pass
[docs]
class HaystackCharField(DRFHaystackFieldMixin, fields.CharField):
pass
[docs]
class HaystackDateField(DRFHaystackFieldMixin, fields.DateField):
pass
[docs]
class HaystackDateTimeField(DRFHaystackFieldMixin, fields.DateTimeField):
pass
[docs]
class HaystackDecimalField(DRFHaystackFieldMixin, fields.DecimalField):
pass
[docs]
class HaystackFloatField(DRFHaystackFieldMixin, fields.FloatField):
pass
[docs]
class HaystackIntegerField(DRFHaystackFieldMixin, fields.IntegerField):
pass
[docs]
class HaystackMultiValueField(DRFHaystackFieldMixin, fields.ListField):
pass
[docs]
class FacetDictField(fields.DictField):
"""
A special DictField which passes the key attribute down to the children's
``to_representation()`` in order to let the serializer know what field they're
currently processing.
"""
[docs]
def to_representation(self, value):
return {str(key): self.child.to_representation(key, val) for key, val in value.items()}
[docs]
class FacetListField(fields.ListField):
"""
The ``FacetListField`` just pass along the key derived from
``FacetDictField``.
"""
[docs]
def to_representation(self, key, data):
return [self.child.to_representation(key, item) for item in data]