Skip to content

Using a dynamically generated schema from an endpoint #778

Answered by vitalik
helmetwearer asked this question in Q&A
Discussion options

You must be logged in to vote

@helmetwearer would you wnat these schemes to be described in OpenAPI documentation json ?

if so - maybe you can do like this, let's say you have 3 types of users: Anonymous, Regular, Admin

class SomeSchemaAnonymous(ModelSchema):
     class Config:
           ... modelf_fields = ['foo', 'bar']

class SomeSchemaRegular(ModelSchema):
     class Config:
           ... modelf_fields = ['foo', 'bar', 'extra']

class SomeSchemaAdmin(ModelSchema):
     class Config:
           ... modelf_fields =  ['foo', 'bar', 'extra', 'secret']


@api.get('/some', response=Union[SomeSchemaAnonymous, SomeSchemaRegular, SomeSchemaAdmin])
def some(request):
     obj = SomeModle.objects.get(...)
     if user.is_a…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@helmetwearer
Comment options

Answer selected by helmetwearer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants