Использование Viewsets для инкапсуляции связанных конечных точек - PullRequest
0 голосов
/ 17 апреля 2020

Мне нравится, как Viewsets могут использовать маршрутизаторы и могут хранить несколько конечных точек вместе без необходимости регистрировать каждый из них в urls.py отдельно.

Я реализую сопоставления для отправки в пользовательский интерфейс для сопоставления коротких кодов которые хранятся в нашей базе данных для отображения имен (из TextChoices). Набор будет продолжать расти ..

from .models import AssetTypes, Countries, Exchanges


    permission_classes = [permissions.IsAuthenticated]

    @action(detail=False, methods=['GET'])
    def asset_types(self, request, pk=None):
        """
        API endpoint that gets the mappings from the two character asset type id that is stored in the database and the
        text to be displayed.
        """
        return JsonResponse(choice_to_json('asset_type', AssetTypes))

    @action(detail=False, methods=['GET'])
    def exchanges(self, request, pk=None):
        """
        API endpoint that gets the mappings from the one character exchange id at is stored in the database and the
        text to be displayed.
        """
        return JsonResponse(choice_to_json('exchange', Exchanges))

    @action(detail=False, methods=['GET'])
    def countries(self, request, pk=None):
        """
        API endpoint that gets the mappings from the two character country id that is stored in the database and the
        text to be displayed.
        """
        return JsonResponse(choice_to_json('country', Countries))

Три возвращают именно то, что я хочу, но если вызывающий пользователь просто использует родительский URL, то возникает ошибка из-за того, что я надеваю У меня нет query_set или вспомогательной модели для этого.

/mapping/counrties # Works as expected
/mapping # No implementation so gets exception

Есть ли подходящий способ реализовать этот сценарий с помощью ViewSet, или мне придется вернуться к @api_view и зарегистрировать каждый отдельно?

...