Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Support custom Querystring parameters #42

Open

Description

@madEng84

I want to append in querystring of some APIs a language key/value so i can translate messages for every “translatable API“.
The js is very strict so the language key, used in my get_queryset or dispatch method and not defined in my view's filters, is not recognized and the call to the API is denied from JS!

@tomchristie how can i resolve this problem according to you (drf filters are strongly coupled with models) ?

Thank you all in advance.

Activity

  1. chrisv2 commented on Nov 10, 2017

    @chrisv2

    I have a similar problem currently, when using the serializer extensions. Serializer extensions use an additional GET parameter expand to specify additional fields to be retrieved. Ex.:

    // get all books
    http://hostname/api/authors/
    // get all books with authors
    http://hostname/api/authors/?expand=author_set
    

    While the JS client does not accept additional querystring parameters out-of-the-box, they can be added for the list action by adding a custom dummy filter backend to the viewset, which does not filter anything, but adds the necessary parameter to the schema like this:

    class SerializerExtensionFilter(BaseFilterBackend):
        def get_schema_fields(self, view):
            return [
                coreapi.Field(name="expand",
                              schema=coreschema.String(
                                  title="expand",
                                  description="Comma-separated list of additional fields to include"
                              ),
                              required=False,
                              location='query'),
            ]
    
        def filter_queryset(self, request, queryset, view):
            # don't filter anything
            return queryset
    
    class MyViewset(viewsets.ModelViewSet):
        queryset = MyModel.objects.all()
        serializer_class = serializers.MySerializer
        filter_backends = (SerializerExtensionFilter,)

    Unfortunately it is not that simple for the read action. Since there is no filtering on read, the schema generator does not add the filter fields in that case. Short of overriding the schema generator itself I see no way to do this right now.

  2. chrisv2 commented on Nov 10, 2017

    @chrisv2

    Update: modifying the generated schema is much easier in DRF 3.7.0:

    http://www.django-rest-framework.org/topics/3.7-announcement/#customizing-api-docs-schema-generation

    So if you can upgrade to 3.7.0 (e.g. you are not on Django 1.8, like me), I'd say a customized schema is the way to go in your scenario.

  3. SirtusKottus commented on Nov 20, 2017

    @SirtusKottus

    If you want more info about your issue, we are discussing that here: #38

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions