Skip to content

Commit

Permalink
refactor(get_permitted_environments): get rid of .distinct() (#4486)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi authored Aug 14, 2024
1 parent 69994eb commit a5197d1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions api/permissions/permission_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ def get_permitted_environments_for_user(
return queryset.prefetch_related("metadata")
return queryset

base_filter = get_base_permission_filter(
environment_ids_from_base_filter = get_object_id_from_base_permission_filter(
user, Environment, permission_key, tag_ids=tag_ids
)
filter_ = base_filter & Q(project=project)
queryset = Environment.objects.filter(
id__in=environment_ids_from_base_filter, project=project
)

queryset = Environment.objects.filter(filter_)
if prefetch_metadata:
queryset = queryset.prefetch_related("metadata")

Expand All @@ -148,7 +149,7 @@ def get_permitted_environments_for_user(
# the select parameters. This leads to an N+1 query for
# lists of environments when description is included, as
# each environment object re-queries the DB seperately.
return queryset.distinct().defer("description")
return queryset.defer("description")


def get_permitted_environments_for_master_api_key(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def test_view_environment_with_staff__query_count_is_expected(
url = reverse("api-v1:environments:environment-list")
data = {"project": project.id}

expected_query_count = 7
expected_query_count = 9
# When
with django_assert_num_queries(expected_query_count):
response = staff_client.get(url, data=data, content_type="application/json")
Expand Down

0 comments on commit a5197d1

Please sign in to comment.