Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 2444798

Browse files
author
Florent
committed
Uses customizable id_field to get django object.
1 parent bcec897 commit 2444798

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

elasticutils/contrib/django/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ class MappingType(BaseMappingType):
197197
`get_model()`.
198198
199199
"""
200+
201+
id_field = 'pk'
202+
200203
def get_object(self):
201204
"""Returns the database object for this result
202205
@@ -205,7 +208,8 @@ def get_object(self):
205208
self.get_model().objects.get(pk=self._id)
206209
207210
"""
208-
return self.get_model().objects.get(pk=self._id)
211+
kwargs = {self.id_field: self._id}
212+
return self.get_model().objects.get(**kwargs)
209213

210214
@classmethod
211215
def get_model(cls):

elasticutils/contrib/django/tasks.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111

1212
@task
13-
def index_objects(mapping_type, ids, chunk_size=100, es=None, index=None):
13+
def index_objects(mapping_type, ids, chunk_size=100, id_field='id', es=None,
14+
index=None):
1415
"""Index documents of a specified mapping type.
1516
1617
This allows for asynchronous indexing.
@@ -48,21 +49,24 @@ def update_in_index(sender, instance, **kw):
4849

4950
# Get the model this mapping type is based on.
5051
model = mapping_type.get_model()
52+
filter_key = '{0}__in'.format(id_field)
5153

5254
# Retrieve all the objects that we're going to index and do it in
5355
# bulk.
5456
for id_list in chunked(ids, chunk_size):
5557
documents = []
5658

57-
for obj in model.objects.filter(id__in=id_list):
59+
for obj in model.objects.filter(**{filter_key: id_list}):
5860
try:
59-
documents.append(mapping_type.extract_document(obj.id, obj))
60-
except Exception as exc:
61+
_id = str(getattr(obj, id_field))
62+
documents.append(mapping_type.extract_document(_id, obj))
63+
except StandardError as exc:
6164
log.exception('Unable to extract document {0}: {1}'.format(
6265
obj, repr(exc)))
6366

6467
if documents:
65-
mapping_type.bulk_index(documents, id_field='id', es=es, index=index)
68+
mapping_type.bulk_index(documents, id_field=id_field, es=es,
69+
index=index)
6670

6771

6872
@task

0 commit comments

Comments
 (0)