diff --git a/apps/orgs/api.py b/apps/orgs/api.py index 63eebb25d..466ac7254 100644 --- a/apps/orgs/api.py +++ b/apps/orgs/api.py @@ -10,7 +10,7 @@ from common.permissions import IsSuperUserOrAppUser from .models import Organization from .serializers import OrgSerializer, OrgReadSerializer, \ OrgMembershipUserSerializer, OrgMembershipAdminSerializer, \ - OrgAllUserSerializer + OrgAllUserSerializer, OrgRetrieveSerializer from users.models import User, UserGroup from assets.models import Asset, Domain, AdminUser, SystemUser, Label from perms.models import AssetPermission @@ -28,10 +28,11 @@ class OrgViewSet(BulkModelViewSet): org = None def get_serializer_class(self): - if self.action in ('list', 'retrieve'): - return OrgReadSerializer - else: - return super().get_serializer_class() + mapper = { + 'list': OrgReadSerializer, + 'retrieve': OrgRetrieveSerializer + } + return mapper.get(self.action, super().get_serializer_class()) def get_data_from_model(self, model): if model == User: diff --git a/apps/orgs/serializers.py b/apps/orgs/serializers.py index fbff6106c..ca5bdddba 100644 --- a/apps/orgs/serializers.py +++ b/apps/orgs/serializers.py @@ -1,12 +1,10 @@ -import re from rest_framework.serializers import ModelSerializer from rest_framework import serializers -from users.models import User, UserGroup +from users.models import UserGroup from assets.models import Asset, Domain, AdminUser, SystemUser, Label from perms.models import AssetPermission from common.serializers import AdaptedBulkListSerializer -from users.serializers import UserOrgSerializer from .utils import set_current_org, get_current_org from .models import Organization from .mixins.serializers import OrgMembershipSerializerMixin @@ -21,9 +19,9 @@ class OrgSerializer(ModelSerializer): class OrgReadSerializer(ModelSerializer): - admins = UserOrgSerializer(many=True, read_only=True) - auditors = UserOrgSerializer(many=True, read_only=True) - users = UserOrgSerializer(many=True, read_only=True) + admins = serializers.SlugRelatedField(slug_field='name', many=True, read_only=True) + auditors = serializers.SlugRelatedField(slug_field='name', many=True, read_only=True) + users = serializers.SlugRelatedField(slug_field='name', many=True, read_only=True) user_groups = serializers.SerializerMethodField() assets = serializers.SerializerMethodField() domains = serializers.SerializerMethodField() @@ -93,3 +91,12 @@ class OrgAllUserSerializer(serializers.Serializer): @staticmethod def get_user_display(obj): return str(obj) + + +class OrgRetrieveSerializer(OrgReadSerializer): + admins = serializers.PrimaryKeyRelatedField(many=True, read_only=True) + auditors = serializers.PrimaryKeyRelatedField(many=True, read_only=True) + users = serializers.PrimaryKeyRelatedField(many=True, read_only=True) + + class Meta(OrgReadSerializer.Meta): + pass