diff --git a/elasticutils/contrib/django/__init__.py b/elasticutils/contrib/django/__init__.py index 1b8892c..f4c031f 100644 --- a/elasticutils/contrib/django/__init__.py +++ b/elasticutils/contrib/django/__init__.py @@ -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) diff --git a/elasticutils/contrib/django/tasks.py b/elasticutils/contrib/django/tasks.py index a61d3df..dd64951 100644 --- a/elasticutils/contrib/django/tasks.py +++ b/elasticutils/contrib/django/tasks.py @@ -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() @@ -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): 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 diff --git a/elasticutils/contrib/django/tests/__init__.py b/elasticutils/contrib/django/tests/__init__.py index 7941d60..b3ce8bc 100644 --- a/elasticutils/contrib/django/tests/__init__.py +++ b/elasticutils/contrib/django/tests/__init__.py @@ -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. @@ -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): @@ -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