Drf-yasg: Is it possible to set example value for fields

Created on 21 May 2018  路  6Comments  路  Source: axnsan12/drf-yasg

Hello,

I am looking to set example attribute of model property but cannot find example in documentation or source code.

Basically, I would like swagger.yaml to create attribute here, like this:

    properties:
      title:
        description: title model help_text
        type: string
        maxLength: 255
        minLength: 1
        example: My title

Then result documentation would include "My title" instead of "string" like in screenshot:

example-title

Is that possible? Would appreciate direction where to look.

duplicate question

Most helpful comment

still could not get it.. i have a similar requirement where i need to show the example request body using drf-ysg request_body, can u pls give me sample code snippet

All 6 comments

Yes, you just have to set the example attribute on an openapi.Schema object (which is just an extended OrderedDict).

How you can get the opportunity to set that is covered in the documentation (override request_bosy, responses, use custom inspectors to process results, etc).

See also #89

Thanks for super fast answer. I will check links and openapi.Schema.

still could not get it.. i have a similar requirement where i need to show the example request body using drf-ysg request_body, can u pls give me sample code snippet

still could not get it.. i have a similar requirement where i need to show the example request body using drf-ysg request_body, can u pls give me sample code snippet

ahahahahah i want know too
it seems can use example only in response

still could not get it.. i have a similar requirement where i need to show the example request body using drf-ysg request_body, can u pls give me sample code snippet

ahahahahah i want know too
it seems can use example only in response

Hi @tjagadeesh @qw2536807, I don't know if you already solved this. But, assuming you creating a custom openapi.Schema for request body, you can just set keyword argument example to any example value you wanted when instantiating new schema.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tiltec picture tiltec  路  4Comments

therefromhere picture therefromhere  路  3Comments

littlehome-eugene picture littlehome-eugene  路  4Comments

geekashu picture geekashu  路  5Comments

csdenboer picture csdenboer  路  3Comments