I have a model that uses django hashid fields for the id
.
class Artwork(Model):
id = HashidAutoField(primary_key=True, min_length=8, alphabet="0123456789abcdefghijklmnopqrstuvwxyz")
title = ....
This is the related item of another model
class ArtworkFeedItem(FeedItem):
artwork = models.OneToOneField('artwork.Artwork', related_name='artwork_feeditem', on_delete=models.CASCADE)
Now I'm trying to setup [django elasticsearch dsl] (https://github.com/django-es/django-elasticsearch-dsl) and to that end have the Document
@registry.register_document
class ArtworkFeedItemDocument(Document):
class Index:
name = 'feed'
settings = {
'number_of_shards': 1,
'number_of_replicas': 0
}
artwork = fields.ObjectField(
properties={
'id': fields.TextField(),
'title': fields.TextField(
attr='title',
fields={
'suggest': fields.Completion(),
}
)
}
)
class Django:
model = ArtworkFeedItem
fields = []
related_models = [Artwork]
However, when I try to rebuild
indices with python manage.py search_index --rebuild
I get the following exception
elasticsearch.exceptions.SerializationError: ({'index': {'_id': Hashid(135): l2vylzm9, '_index': 'feed'}}, TypeError("Unable to serialize Hashid(135): l2vylzm9 (type: <class 'hashid_field.hashid.Hashid'>)",))
Django elasticsearch dsl clearly does not know what to do with such a hashid field.
I thought maybe I could just make my own HashIdField
like
from elasticsearch_dsl import Field
class HashIdField(Field):
"""
Custom DSL field to support HashIds
"""
name = "hashid"
def _serialize(self, data):
return data.hashid
then use it in 'id': HashIdField
, but this gave me yet another exception
elasticsearch.exceptions.RequestError: RequestError(400, 'mapper_parsing_exception', 'No handler for type [hashid] declared on field [id]')
Does anyone know how I could get this to work?
For anyone interested, I managed to solve this by overriding the generate_id
method of the Document
so that the _id
used is just a plain string:
@classmethod
def generate_id(cls, object_instance):
return object_instance.id.hashid