You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 18, 2019. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 18, 2019. It is now read-only.
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) ?
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:
classSerializerExtensionFilter(BaseFilterBackend):
defget_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'),
]
deffilter_queryset(self, request, queryset, view):
# don't filter anythingreturnquerysetclassMyViewset(viewsets.ModelViewSet):
queryset=MyModel.objects.all()
serializer_class=serializers.MySerializerfilter_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.
Activity
chrisv2 commentedon Nov 10, 2017
I have a similar problem currently, when using the serializer extensions. Serializer extensions use an additional GET parameter
expandto specify additional fields to be retrieved. Ex.:While the JS client does not accept additional querystring parameters out-of-the-box, they can be added for the
listaction 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:Unfortunately it is not that simple for the
readaction. Since there is no filtering onread, 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 commentedon 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 commentedon Nov 20, 2017
If you want more info about your issue, we are discussing that here: #38