Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion elasticutils/contrib/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,4 @@ def get_indexable(cls):

"""
model = cls.get_model()
return model.objects.order_by('id').values_list('id', flat=True)
return model.objects.order_by('pk').values_list('pk', flat=True)
12 changes: 6 additions & 6 deletions elasticutils/contrib/django/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def update_in_index(sender, instance, **kw):
return

log.debug('Indexing objects {0}-{1}. [{2}]'.format(
ids[0], ids[-1], len(ids)))
ids[0], ids[-1], len(ids)))

# Get the model this mapping type is based on.
model = mapping_type.get_model()
Expand All @@ -54,15 +54,15 @@ def update_in_index(sender, instance, **kw):
for id_list in chunked(ids, chunk_size):
documents = []

for obj in model.objects.filter(id__in=id_list):
for obj in model.objects.filter(pk__in=id_list):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to change this here, you have to change it everywhere else, too. Currently, ElasticUtils requires that you use id and won't work with non-id primary keys.

try:
documents.append(mapping_type.extract_document(obj.id, obj))
except StandardError as exc:
documents.append(mapping_type.extract_document(obj.pk, obj))
except Exception as exc:
log.exception('Unable to extract document {0}: {1}'.format(
obj, repr(exc)))
obj, repr(exc)))

if documents:
mapping_type.bulk_index(documents, id_field='id', es=es, index=index)
mapping_type.bulk_index(documents, id_field=model._meta.pk.name, es=es, index=index)


@task
Expand Down
14 changes: 12 additions & 2 deletions elasticutils/contrib/django/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ def reset_model_cache():
del _model_cache[0:]


class FakePK():
name = 'id'


class Meta(object):
def __init__(self, db_table):
self.db_table = db_table

pk = FakePK()


class SearchQuerySet(object):
# Yes. This is kind of crazy, but ... whatever.
Expand All @@ -28,8 +34,8 @@ def get(self, pk):
pk = int(pk)
return [m for m in _model_cache if m.id == pk][0]

def filter(self, id__in=None):
self.steps.append(('filter', id__in))
def filter(self, pk__in=None):
self.steps.append(('filter', pk__in))
return self

def order_by(self, *fields):
Expand Down Expand Up @@ -89,6 +95,10 @@ def __init__(self, **kw):
setattr(self, key, kw[key])
_model_cache.append(self)

@property
def pk(self):
return self.id


class FakeDjangoMappingType(MappingType, Indexable):
@classmethod
Expand Down