Skip to content

Commit

Permalink
Merge pull request #215 from algolia/feat/propertyObjectID
Browse files Browse the repository at this point in the history
Add @Property ObjectId support
  • Loading branch information
PLNech authored Aug 9, 2017
2 parents cc424e0 + 56a7df8 commit 5d30c05
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion algoliasearch_django/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(self, model, client, settings):
self.fields)))

# Check custom_objectID
if self.custom_objectID in chain(['pk'], all_model_fields):
if self.custom_objectID in chain(['pk'], all_model_fields) or hasattr(model, self.custom_objectID):
self.objectID = get_model_attr(self.custom_objectID)
else:
raise AlgoliaIndexError('{} is not a model field of {}'.format(
Expand Down
4 changes: 4 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class User(models.Model):
_lng = models.FloatField(default=0)
_permissions = models.CharField(max_length=30, blank=True)

@property
def reverse_username(self):
return self.username[::-1]

def location(self):
return self._lat, self._lng

Expand Down
8 changes: 8 additions & 0 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ class UserIndex(AlgoliaIndex):
obj = index.get_raw_record(self.user)
self.assertEqual(obj['objectID'], 'algolia')

def test_custom_objectID_property(self):
class UserIndex(AlgoliaIndex):
custom_objectID = 'reverse_username'

index = UserIndex(User, self.client, settings.ALGOLIA)
obj = index.get_raw_record(self.user)
self.assertEqual(obj['objectID'], 'ailogla')

def test_invalid_custom_objectID(self):
class UserIndex(AlgoliaIndex):
custom_objectID = 'uid'
Expand Down

0 comments on commit 5d30c05

Please sign in to comment.