Skip to content

Commit 5d30c05

Browse files
authored
Merge pull request #215 from algolia/feat/propertyObjectID
Add @Property ObjectId support
2 parents cc424e0 + 56a7df8 commit 5d30c05

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

algoliasearch_django/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __init__(self, model, client, settings):
125125
self.fields)))
126126

127127
# Check custom_objectID
128-
if self.custom_objectID in chain(['pk'], all_model_fields):
128+
if self.custom_objectID in chain(['pk'], all_model_fields) or hasattr(model, self.custom_objectID):
129129
self.objectID = get_model_attr(self.custom_objectID)
130130
else:
131131
raise AlgoliaIndexError('{} is not a model field of {}'.format(

tests/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class User(models.Model):
1111
_lng = models.FloatField(default=0)
1212
_permissions = models.CharField(max_length=30, blank=True)
1313

14+
@property
15+
def reverse_username(self):
16+
return self.username[::-1]
17+
1418
def location(self):
1519
return self._lat, self._lng
1620

tests/test_index.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ class UserIndex(AlgoliaIndex):
8080
obj = index.get_raw_record(self.user)
8181
self.assertEqual(obj['objectID'], 'algolia')
8282

83+
def test_custom_objectID_property(self):
84+
class UserIndex(AlgoliaIndex):
85+
custom_objectID = 'reverse_username'
86+
87+
index = UserIndex(User, self.client, settings.ALGOLIA)
88+
obj = index.get_raw_record(self.user)
89+
self.assertEqual(obj['objectID'], 'ailogla')
90+
8391
def test_invalid_custom_objectID(self):
8492
class UserIndex(AlgoliaIndex):
8593
custom_objectID = 'uid'

0 commit comments

Comments
 (0)