fix: 修改 BitChoicesField to_representation swagger 调用报错的问题

pull/9075/head
Jiangjie.Bai 2022-11-15 14:59:22 +08:00
parent 22dd9906c7
commit 582a8e0c52
3 changed files with 13 additions and 3 deletions

View File

@ -120,6 +120,13 @@ class BitChoicesField(TreeChoicesMixin, serializers.MultipleChoiceField):
super().__init__(choices=choices, **kwargs)
def to_representation(self, value):
if isinstance(value, list) and len(value) == 1:
# Swagger 会使用 field.choices.keys() 迭代传递进来
return [
{"value": c.name, "label": c.label}
for c in self._choice_cls
if c.name == value[0]
]
return [
{"value": c.name, "label": c.label}
for c in self._choice_cls

View File

@ -16,7 +16,7 @@ __all__ = ["AssetPermissionSerializer", "ActionChoicesField"]
class ActionChoicesField(BitChoicesField):
def __init__(self, **kwargs):
super().__init__(ActionChoices, **kwargs)
super().__init__(choice_cls=ActionChoices, **kwargs)
class AssetPermissionSerializer(BulkOrgResourceModelSerializer):

View File

@ -65,12 +65,15 @@ user_permission_urlpatterns = [
# 收藏的资产
path('<uuid:pk>/nodes/favorite/assets/', api.UserFavoriteGrantedAssetsApi.as_view(), name='user-ungrouped-assets'),
path('nodes/favorite/assets/', api.MyFavoriteGrantedAssetsApi.as_view(), name='my-ungrouped-assets'),
path('nodes/favorite/assets/', api.MyFavoriteGrantedAssetsApi.as_view(),
name='my-ungrouped-assets'),
# 获取授权给用户某个资产的所有账号
path('<uuid:pk>/assets/<uuid:asset_id>/accounts/', api.UserGrantedAssetAccountsApi.as_view(),
name='user-asset-accounts'),
path('assets/<uuid:asset_id>/accounts/', api.MyGrantedAssetAccountsApi.as_view(), name='my-asset-accounts'),
path('/((?P<user>[^/.]+)/)?assets/<uuid:asset_id>/accounts/',
api.UserGrantedAssetAccountsApi.as_view(),
name='my-asset-accounts'),
]
user_group_permission_urlpatterns = [