Skip to content

Instantly share code, notes, and snippets.

@wonderbeyond
Last active February 1, 2021 07:08
Show Gist options
  • Save wonderbeyond/6591b10c6a1ceaf5c6aa6af588d7a931 to your computer and use it in GitHub Desktop.
Save wonderbeyond/6591b10c6a1ceaf5c6aa6af588d7a931 to your computer and use it in GitHub Desktop.
Django-rest-framework metadata endpoint example
class ExampleViewSet(PartialUpdateModelMixin, viewsets.ReadOnlyModelViewSet):
    ...

    @action(detail=False, methods=["GET"], url_path="_meta")
    def api_meta(self, request):
        meta = {
            "fields": {
                "types": {
                    "choices": Example.ExampleType.choices
                },
                "method": {
                    "choices": Example.ExampleMethod.choices
                }
            }
        }
        return Response(meta)

In this example, our model have a "types" field, it's an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment