1

I describe the structure of the outgoing JSON in the model class, however I cannot make the output as a list.

My model class:

class versions_info(BaseModel):
    """ List of versions """
    version : str = Field(..., title="Version",example="2.1.1")
    url :   str = Field(..., title="Url",example="https://ocpi.wedwe.ww/ocpi/2.1.1/")

And in the documentation I see:

enter image description here

However, I need it to be displayed as:

[
  {
    "version": "2.1.1",
    "url": "https://www.server.com/ocpi/2.1.1/"
  },
  {
    "version": "2.2",
    "url": "https://www.server.com/ocpi/2.2/"
  }
]

What am I doing wrong?

3
  • 3
    Since you didn't include your route definition it's hard to say - but usually you do @app.get('/foo', response_model=List[versions_info]) to indicate that you're returning a list instead of a single object. Commented Sep 8, 2022 at 13:09
  • Hi Pavel You are creating ocpi integration for existing central system as well written in Python ? Commented Oct 9, 2022 at 17:06
  • Yes,we are the developers of the electric charging stations platform Commented Oct 12, 2022 at 4:51

1 Answer 1

3

You can indicate that you're returning a List by wrapping the response_model you've defined in List[<model>].

So in your case it'd be:

@app.get('/foo', response_model=List[versions_info])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.