Skip to content

Commit 02baf58

Browse files
authored
Merge pull request #55 from tom-pollak/dev/default-get-fix
fixes #52
2 parents 2b42243 + 6d61045 commit 02baf58

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

fastlite/kw.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def ids_and_rows_where(
5656
yield row.pop('__rid'), row
5757

5858
@patch
59-
def get(self:Table, pk_values: list|tuple|str|int, as_cls:bool=True, xtra:dict|None=None)->Any:
59+
def get(self:Table, pk_values: list|tuple|str|int, as_cls:bool=True, xtra:dict|None=None, default:Any=UNSET)->Any:
6060
if not isinstance(pk_values, (list, tuple)): pk_values = [pk_values]
6161
last_pk = pk_values[0] if len(self.pks) == 1 else pk_values
6262
if not xtra: xtra = getattr(self, 'xtra_id', {})
@@ -65,7 +65,9 @@ def get(self:Table, pk_values: list|tuple|str|int, as_cls:bool=True, xtra:dict|N
6565
if len(pks)!=len(vals): raise NotFoundError(f"Need {len(pks)} pk")
6666
wheres = ["[{}] = ?".format(pk_name) for pk_name in pks]
6767
item = first(self.ids_and_rows_where(" and ".join(wheres), vals))
68-
if not item: raise NotFoundError
68+
if not item:
69+
if default is UNSET: raise NotFoundError()
70+
return default
6971
rid,row = item
7072
self.last_pk,self.last_rowid = last_pk,rid
7173
if as_cls and hasattr(self,'cls'): row = self.cls(**row)

0 commit comments

Comments
 (0)