From 4545709ede1c94ad83d6f1d129c89464e715153b Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Tue, 7 Jan 2025 13:19:25 +0200 Subject: [PATCH] Fix getting the list of available teams per organization The problem comes from the fact that `teamname` is a runtime property of the `Team` class and not an actual database field. In addition we were using `Team.object.values()` which returns an `Iterable[dict]`, instead of `Iterable[Team]`. So even if it returned the right type, it would have been terribly slow, due to N+1 situation. This problem made the `/user//organization` return an error, therefore prevented QFieldSync from selecting the available organizations for the team. --- docker-app/qfieldcloud/core/serializers.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/docker-app/qfieldcloud/core/serializers.py b/docker-app/qfieldcloud/core/serializers.py index b7f688591..90b9d1b23 100644 --- a/docker-app/qfieldcloud/core/serializers.py +++ b/docker-app/qfieldcloud/core/serializers.py @@ -187,10 +187,13 @@ def get_members(self, obj): def get_teams(self, obj: Organization) -> list[str]: """Implementation of `SerializerMethodField` for `teams`. Returns list of team names.""" - return [ - t.teamname - for t in Team.objects.filter(team_organization=obj).values("username") - ] + team_qs = ( + Team.objects.filter(team_organization=obj) + .select_related("team_organization") + .only("username", "team_organization__username") + ) + + return [t.teamname for t in team_qs] def get_avatar_url(self, obj): return get_avatar_url(obj)