Skip to content

Commit

Permalink
Account for multiple entities in primary key lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
huntfx committed Feb 15, 2021
1 parent 259b77a commit bcc9c95
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ftrack_query/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

__all__ = ['FTrackQuery', 'entity', 'and_', 'or_', 'not_', 'event']

__version__ = '1.6.2'
__version__ = '1.6.3'

import ftrack_api

Expand Down
12 changes: 11 additions & 1 deletion ftrack_query/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,22 @@ def __call__(self, *args, **kwargs):
In rare cases, it can be valid to pass in a single argument,
such as User('username'). The inspiration for this was taken
from the old API.
As the FTrack database is case insensitive, in the case of
multiple results, use Python to find the exact match.
"""
if self._entity in self._PrimaryKeys and len(args) == 1 and not kwargs:
key = self._PrimaryKeys[self._entity]
value = args[0]
try:
return self.where(**{self._PrimaryKeys[self._entity]: args[0]}).one()
return self.where(**{key: value}).one()
except ftrack_api.exception.NoResultFoundError:
return None
except ftrack_api.exception.MultipleResultsFoundError:
for result in self.where(**{key: value}):
if result[key] == value:
return result
raise


raise TypeError("'Query' object is not callable, "
"perhaps you meant to use 'Query.where()'?")
Expand Down

0 comments on commit bcc9c95

Please sign in to comment.