Skip to content

Commit

Permalink
Fix issue w/model_to_dict when only aliases present.
Browse files Browse the repository at this point in the history
Fixes #2821
  • Loading branch information
coleifer committed Jan 5, 2024
1 parent 437c4c2 commit d7d13d6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions playhouse/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def model_to_dict(model, recurse=True, backrefs=False, only=None,
should_skip = lambda n: (n in exclude) or (only and (n not in only))

if fields_from_query is not None:
only.add('__sentinel__') # Add a placeholder to make non-empty.
for item in fields_from_query._returning:
if isinstance(item, Field):
only.add(item)
Expand Down
12 changes: 12 additions & 0 deletions tests/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,18 @@ def test_fields_from_query(self):
{'magic': 1337, 'content': 'u0-0',
'user': {'username': 'u0'}})

def test_fields_from_query_alias(self):
q = User.select(User.username.alias('name'))
res = q[0]
self.assertEqual(model_to_dict(res, fields_from_query=q),
{'name': 'peewee'})

UA = User.alias()
q = UA.select(UA.username.alias('name'))
res = q[0]
self.assertEqual(model_to_dict(res, fields_from_query=q),
{'name': 'peewee'})

def test_only_backref(self):
for i in range(3):
Tweet.create(user=self.user, content=str(i))
Expand Down

0 comments on commit d7d13d6

Please sign in to comment.