From d7d13d6a5b6e563f3b2003338b2900c639c4ca27 Mon Sep 17 00:00:00 2001 From: Charles Leifer Date: Fri, 5 Jan 2024 07:36:04 -0600 Subject: [PATCH] Fix issue w/model_to_dict when only aliases present. Fixes #2821 --- playhouse/shortcuts.py | 1 + tests/shortcuts.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/playhouse/shortcuts.py b/playhouse/shortcuts.py index cba882ba0..d2dfc52a8 100644 --- a/playhouse/shortcuts.py +++ b/playhouse/shortcuts.py @@ -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) diff --git a/tests/shortcuts.py b/tests/shortcuts.py index f769c22a3..4d2bec54e 100644 --- a/tests/shortcuts.py +++ b/tests/shortcuts.py @@ -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))