From 90f7724e8ec232d6635f318c247b063b45fcf580 Mon Sep 17 00:00:00 2001 From: voidZXL Date: Fri, 23 Aug 2024 11:26:14 +0800 Subject: [PATCH] fix relation serialize issue when relation is empty --- tests/server/app/schema.py | 1 + tests/test_1_orm/test_schema_query.py | 8 ++++++-- utilmeta/core/orm/fields/field.py | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/server/app/schema.py b/tests/server/app/schema.py index 74351c5..3788c26 100644 --- a/tests/server/app/schema.py +++ b/tests/server/app/schema.py @@ -172,6 +172,7 @@ async def get_top_articles(cls, *pks): articles: List[ArticleBase] = orm.Field('contents__article') # test multi+fk + follower_names: List[str] = orm.Field('followers.username') # @property # def total_views(self) -> int: # return sum([article.views for article in self.articles]) diff --git a/tests/test_1_orm/test_schema_query.py b/tests/test_1_orm/test_schema_query.py index 8325372..d28a779 100644 --- a/tests/test_1_orm/test_schema_query.py +++ b/tests/test_1_orm/test_schema_query.py @@ -18,6 +18,7 @@ def test_serialize_users(self, service): assert len(res) == 2 assert res[0]["username"] == "alice" assert res[0]["followers_num"] == 2 + assert set(res[0].follower_names) == {'bob', 'jack'} assert res[0]["followings_num"] == 1 assert set(res[0]["liked_slugs"]) == {"about-tech", "some-news", "big-shot"} assert res[0]["@views"] == 103 @@ -41,6 +42,7 @@ def test_serialize_users(self, service): sup = UserSchema.init(5) assert len(sup.articles) == 0 assert sup.articles_num == 0 + assert sup.follower_names == [] def test_scope_and_excludes(self): from app.schema import UserSchema, UserQuery @@ -87,8 +89,8 @@ def test_init_articles(self, service): assert content.article.id == 1 def test_related_qs(self): - from app.schema import UserBase, ArticleSchema, UserQuery - from app.models import Article, Follow, User + from app.schema import UserBase, ArticleSchema + from app.models import Article, User from typing import List, Optional from utilmeta.core import orm from django.db import models @@ -155,6 +157,7 @@ async def test_async_init_users(self): assert user.sum_views == 103 assert user.top_articles[0].author_tag["name"] == "alice" assert user.top_articles[0].views == 103 + assert set(user.follower_names) == {'bob', 'jack'} # -------------- bob = await UserSchema.ainit( @@ -170,6 +173,7 @@ async def test_async_init_users(self): sup = UserSchema.init(5) assert len(sup.articles) == 0 assert sup.articles_num == 0 + assert sup.follower_names == [] @pytest.mark.asyncio async def test_async_init_users_with_sync_query(self): diff --git a/utilmeta/core/orm/fields/field.py b/utilmeta/core/orm/fields/field.py index 5a2ba3a..2468d69 100644 --- a/utilmeta/core/orm/fields/field.py +++ b/utilmeta/core/orm/fields/field.py @@ -236,6 +236,7 @@ def setup(self, options: utype.Options): # if not self.model_field.is_exp: # expression need to be isolated, otherwise multiple many included query will blow the query self.isolated = True + self.related_single = False elif not self.model_field.is_concrete: self.isolated = True