Skip to content

Commit 7e959bb

Browse files
safwanrahmanXu Peng
and
Xu Peng
authored
custom model fields mapping (#449)
* hook for custom model field class to es dsl field class @XuPeng-SH * Adding documentations --------- Co-authored-by: Xu Peng <[email protected]>
1 parent 32bc7e7 commit 7e959bb

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

django_elasticsearch_dsl/documents.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ def prepare(self, instance):
147147
}
148148
return data
149149

150+
@classmethod
151+
def get_model_field_class_to_field_class(cls):
152+
"""
153+
Returns dict of relationship from model field class to elasticsearch
154+
field class
155+
156+
You may want to override this if you have model field class not included
157+
in model_field_class_to_field_class.
158+
"""
159+
return model_field_class_to_field_class
160+
150161
@classmethod
151162
def to_field(cls, field_name, model_field):
152163
"""
@@ -155,7 +166,7 @@ def to_field(cls, field_name, model_field):
155166
model field to ES field logic
156167
"""
157168
try:
158-
return model_field_class_to_field_class[
169+
return cls.get_model_field_class_to_field_class()[
159170
model_field.__class__](attr=field_name)
160171
except KeyError:
161172
raise ModelFieldNotMappedError(

docs/source/fields.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,40 @@ Available Fields
234234
instance.
235235

236236

237+
Field Mapping
238+
=============
239+
Django Elasticsearch DSL maps most of the django fields
240+
appropriate Elasticsearch Field. You can find the field
241+
mapping on `documents.py` file in the `model_field_class_to_field_class`
242+
variable. If you need to change the behavior of this mapping, or add mapping
243+
for your custom field, you can do so by overwriting the classmethod
244+
`get_model_field_class_to_field_class`. Remember, you need to inherit
245+
`django_elasticsearch_dsl.fields.DEDField` for your custom field.
246+
Like following
247+
248+
.. code-block:: python
249+
250+
from django_elasticsearch_dsl.fields import DEDField
251+
252+
class MyCustomDEDField(DEDField, ElasticsearchField):
253+
pass
254+
255+
@classmethod
256+
def get_model_field_class_to_field_class(cls):
257+
field_mapping = super().get_model_field_class_to_field_class()
258+
field_mapping[MyCustomDjangoField] = MyCustomDEDField
259+
260+
237261
Document id
238262
===========
239263

240-
The elasticsearch document id (``_id``) is not strictly speaking a field, as it is not
264+
The elasticsearch document id (``_id``) is not strictly speaking a field, as it is not
241265
part of the document itself. The default behavior of ``django_elasticsearch_dsl``
242266
is to use the primary key of the model as the document's id (``pk`` or ``id``).
243267
Nevertheless, it can sometimes be useful to change this default behavior. For this, one
244268
can redefine the ``generate_id(cls, instance)`` class method of the ``Document`` class.
245269

246-
For example, to use an article's slug as the elasticsearch ``_id`` instead of the
270+
For example, to use an article's slug as the elasticsearch ``_id`` instead of the
247271
article's integer id, one could use:
248272

249273
.. code-block:: python

0 commit comments

Comments
 (0)