How to display multiple value parameter in django rest framework swagger? - django

Versions:
coreapi==2.3.1
Django==1.11.2
django-rest-framework==0.1.0
django-rest-swagger==2.1.2
django-silk==1.0.0
djangorestframework==3.6.3
API endpoint: /search?filter=("pages")?filter=("people")
In my SwaggerSchemaView I have defined the filter field as follows:
coreapi.Field(name="filters", description="=search(query)", location="query", type="array", required=True)
Swagger looks like this:
now when i provide the values in the value box, it results in the api of format:
/search?filter=("pages"),("people")
How can I make swagger respect the format I want that is
/search?filter=("pages")?filter=("people")

What you need is not supported by CoreAPI out of the box. It will be after this issue gets fixed - https://github.com/core-api/python-openapi-codec/issues/25
But it is available in OpenAPI(Swagger) using style and explode params - https://swagger.io/specification/
I would recommend writing your own swagger yaml instead of generating it from DRF as DRF is not aligned with Swagger to be able to extract all the required information from it.

Related

Where is my "raw data & HTML form" option in django rest api?

Hellow developers,
I started using django rest framework, but I am not able to see raw data/ HTML form options in my post request user interface. My dhango and rest framework versions are latest.
The whole API works fine when I insert the data as json inside the content textarea, but I would like to have another options as well.
Found the answer
using decorators (#api_view) will not generate those ui options,
but using inheritated classes like (APIview) will give you those options.

How to document response schema for Django REST Framework documentaion?

I'm documenting a custom endpoint using default DRF documentation API. The output of the endpoint is quite complex and I need a pretty way to display it to the frontend-developers in documentation API. Current solution is to use method's docstring which is not that pretty.
There is a clean way to describe input parameters using schema, but I'm unable to find examples to describe schema for the output.
There is screenshot in the official documentation that indicates that it is possible (note a response schema at the picture), but unfortunately no example.
I was looking for the same functionality but it seems like most of this response stuff is auto-generated with little control over it. E.g., my setup kept insisting that all my POSTs must return a 201 and no way to specify a custom response body, just some object that it decided this POST is creating.
Ended up using drf-yasg instead, it was super-easy to get started and specify responses, e.g.:
user_response = openapi.Response('response description', UserSerializer)

How to use Django Rest API in Django normal project for autocomplete using Select2?

In my Django project, I have a model named Product. The model consists of products which have following entities:
name, id, price and so on.
In my project, an admin can add a new/old product anytime.
Now, for searching, I want to add autocomplete. I want to use Select2.
So users don't have to memorize the name of the products. To do that I found out here in the Select2 doc
that :
Select2 comes with AJAX support built in, using jQuery's AJAX methods
With this, I can search an API and fetch the data to show the users in autocomplete search field.
My Question:
Should I create a Django rest API and use that API to store products and fetch the data?
1.1 Would it be wise?
1.2 Is it possible to make a rest API within a normal Django project? If not then how to do that?
Or should I just use a normal urls.py and querying the result from
Select2 ajax function to that urls.py and to a custom query.py and
fetch the data directly from the database?
I don't think using rest framework with normal Django project would cause any problem. You are just adding some extra urls, that's all. It won't cause any problem to your project. Moreover, you can use the API to get json data of various models.
Hope this helps.

use django-rest-swagger 2 with custom swagger.json

I have a project build with django-rest-framework, and I want to use django-rest-swagger to get API documentation, so I made a swagger.json file via swagger editor, then my question is:
How can I make django-rest-swagger read and render my own swagger.json instead of auto-generated from code?
I've checked the django-rest-swagger doc over and over again but nothing found about that.
Any comment will be appreciated.
I know this is old post, but I ran into the same issue and wanted to provide my work around.
If you are trying to create a Swagger UI from an external JSON, this was my work around. I am using django but wanted to provide the swagger api of another framework and server. Here are two options:
The simplest solution is to just manually render the swagger html template and insert the endpoint url that provides the JSON inside SwaggerUi(), this is generally located in the last block.
Alternatively, if you cannot access the json directly or have a static file, create your own rest end point that either reads the file, or makes the request to the remote server, and then itself serves up the desired JSON. Reference this endpoint in your swagger template.

Django REST Framework browsable api filter controls not showing

I followed the docs to add filtering to my API, installed django-filter, django-crispy-forms (added to INSTALLED_APPS) and using filter_backends/filter_fields/search_fields I can use the filtering using query parameters. However, the docs say: "Generic filters can also present themselves as HTML controls in the browsable API" and "the browsable API will present a filtering control for DjangoFilterBackend, like so:". My question is how? I don't see any additional controls for filtering/search. I'm using DRF 3.2.4.
Filters displaying as HTML controls on the browsable API is a feature of Django Rest Framework versions 3.3.0 and above.
http://www.django-rest-framework.org/topics/3.3-announcement/
It is unfortunate that they do not version their documentation.