Skip to content

Commit

Permalink
custom model fields mapping (#449)
Browse files Browse the repository at this point in the history
* hook for custom model field class to es dsl field class @XuPeng-SH

* Adding documentations

---------

Co-authored-by: Xu Peng <xupeng@tictalk.com>
  • Loading branch information
safwanrahman and Xu Peng committed May 17, 2023
1 parent 32bc7e7 commit 7e959bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
13 changes: 12 additions & 1 deletion django_elasticsearch_dsl/documents.py
Expand Up @@ -147,6 +147,17 @@ def prepare(self, instance):
}
return data

@classmethod
def get_model_field_class_to_field_class(cls):
"""
Returns dict of relationship from model field class to elasticsearch
field class
You may want to override this if you have model field class not included
in model_field_class_to_field_class.
"""
return model_field_class_to_field_class

@classmethod
def to_field(cls, field_name, model_field):
"""
Expand All @@ -155,7 +166,7 @@ def to_field(cls, field_name, model_field):
model field to ES field logic
"""
try:
return model_field_class_to_field_class[
return cls.get_model_field_class_to_field_class()[
model_field.__class__](attr=field_name)
except KeyError:
raise ModelFieldNotMappedError(
Expand Down
28 changes: 26 additions & 2 deletions docs/source/fields.rst
Expand Up @@ -234,16 +234,40 @@ Available Fields
instance.


Field Mapping
=============
Django Elasticsearch DSL maps most of the django fields
appropriate Elasticsearch Field. You can find the field
mapping on `documents.py` file in the `model_field_class_to_field_class`
variable. If you need to change the behavior of this mapping, or add mapping
for your custom field, you can do so by overwriting the classmethod
`get_model_field_class_to_field_class`. Remember, you need to inherit
`django_elasticsearch_dsl.fields.DEDField` for your custom field.
Like following

.. code-block:: python
from django_elasticsearch_dsl.fields import DEDField
class MyCustomDEDField(DEDField, ElasticsearchField):
pass
@classmethod
def get_model_field_class_to_field_class(cls):
field_mapping = super().get_model_field_class_to_field_class()
field_mapping[MyCustomDjangoField] = MyCustomDEDField
Document id
===========

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

For example, to use an article's slug as the elasticsearch ``_id`` instead of the
For example, to use an article's slug as the elasticsearch ``_id`` instead of the
article's integer id, one could use:

.. code-block:: python
Expand Down

0 comments on commit 7e959bb

Please sign in to comment.