Skip to content

Commit 0c7e3b6

Browse files
committed
Add support for non-pk IDs to get_qualified_id
The `CustomQuerySetIndex` `get_qualified_id` method now takes the full record object to help formulate the ID, rather than assuming it's the record PK. This adds support for using alternative fields as IDs (in Solr) on subclasses; they can override that method to provide a different ID. Fixtures in the main conftest.py file have been updated as well to utilize `get_qualified_id` rather than putting together the ID manually. (I think this was always my intention, but I forgot to implement it.)
1 parent 589a517 commit 0c7e3b6

2 files changed

Lines changed: 4 additions & 6 deletions

File tree

django/sierra/base/search_indexes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ def __init__(self, queryset=None, using=None):
9898
def get_django_ct(self):
9999
return utils.get_model_ct(self.get_model())
100100

101-
def get_qualified_id(self, _id):
102-
return '{}.{}'.format(self.get_django_ct(), _id)
101+
def get_qualified_id(self, record):
102+
return '{}.{}'.format(self.get_django_ct(), record.pk)
103103

104104
def get_backend(self, using=None):
105105
using = using or self.using
@@ -163,7 +163,7 @@ def delete(self, using=None, commit=True, queryset=None):
163163
backend = self.get_backend(using)
164164
queryset = self.index_queryset() if queryset is None else queryset
165165
if backend is not None:
166-
ids_to_delete = [self.get_qualified_id(r.pk) for r in queryset]
166+
ids_to_delete = [self.get_qualified_id(r) for r in queryset]
167167
backend.conn.delete(id=ids_to_delete, commit=commit)
168168

169169
def clear(self, using=None, commit=True):

django/sierra/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,13 @@ def get_records_from_index(solr_conns, solr_search):
566566
"""
567567
def _get_records_from_index(index, record_set, results=None):
568568
id_fname = index.reserved_fields['haystack_id']
569-
meta = index.get_model()._meta
570569
if results is None:
571570
conn = solr_conns[getattr(index, 'using', 'default')]
572571
results = solr_search(conn, '*')
573572

574573
found_records = {}
575574
for record in record_set:
576-
cmp_id = '{}.{}.{}'.format(meta.app_label, meta.model_name,
577-
record.pk)
575+
cmp_id = index.get_qualified_id(record)
578576
matches = [r for r in results if r[id_fname] == cmp_id]
579577
if len(matches) == 1:
580578
found_records[record.pk] = matches[0]

0 commit comments

Comments
 (0)