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

Support custom Querystring parameters #42

Open
madEng84 opened this issue Oct 19, 2017 · 3 comments
Open

Support custom Querystring parameters #42

madEng84 opened this issue Oct 19, 2017 · 3 comments

Comments

@madEng84
Copy link

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.

@chrisv2
Copy link

chrisv2 commented Nov 10, 2017

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.

@chrisv2
Copy link

chrisv2 commented Nov 10, 2017

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.

@SirtusKottus
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants