0
0
FastAPIframework~10 mins

Example data in schema in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define example data in a Pydantic schema.

FastAPI
from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int

    class Config:
        schema_extra = {"example": [1] }
Drag options to blanks, or click blank then click option'
A{"name": "Alice", "age": 30}
B{"name": "Bob", "age": 25}
C{"name": "Charlie", "age": 40}
D{"name": "Diana", "age": 22}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a dictionary for example data.
Not matching keys to model fields.
Using incorrect syntax for the example dictionary.
2fill in blank
medium

Complete the code to add example data for a Product schema.

FastAPI
from pydantic import BaseModel

class Product(BaseModel):
    id: int
    name: str
    price: float

    class Config:
        schema_extra = {"example": [1] }
Drag options to blanks, or click blank then click option'
A{"id": 1, "name": "Laptop", "price": "cheap"}
B{"id": "one", "name": "Laptop", "price": 999.99}
C{"id": 1, "name": 123, "price": 999.99}
D{"id": 1, "name": "Laptop", "price": 999.99}
Attempts:
3 left
💡 Hint
Common Mistakes
Using string instead of int for id.
Using number instead of string for name.
Using string instead of float for price.
3fill in blank
hard

Fix the error in the example data for the Address schema.

FastAPI
from pydantic import BaseModel

class Address(BaseModel):
    street: str
    city: str
    zipcode: str

    class Config:
        schema_extra = {"example": [1] }
Drag options to blanks, or click blank then click option'
A{"street": "123 Main St", "city": "Springfield", "zipcode": "12345"}
B{"street": "123 Main St", "city": "Springfield", "zipcode": 12345}
C{"street": 123, "city": "Springfield", "zipcode": "12345"}
D{"street": "123 Main St", "city": 456, "zipcode": "12345"}
Attempts:
3 left
💡 Hint
Common Mistakes
Using an integer for zipcode instead of a string.
Using numbers for street or city fields.
4fill in blank
hard

Fill both blanks to add example data for a BlogPost schema with title and tags.

FastAPI
from pydantic import BaseModel
from typing import List

class BlogPost(BaseModel):
    title: str
    tags: List[str]

    class Config:
        schema_extra = {"example": {"title": [1], "tags": [2]
Drag options to blanks, or click blank then click option'
A"My First Post"
B["python", "fastapi"]
C"Another Post"
D["java", "spring"]
Attempts:
3 left
💡 Hint
Common Mistakes
Using list for title or string for tags.
Not quoting the string values.
5fill in blank
hard

Fill all three blanks to add example data for a UserProfile schema with username, email, and active status.

FastAPI
from pydantic import BaseModel

class UserProfile(BaseModel):
    username: str
    email: str
    active: bool

    class Config:
        schema_extra = {"example": {"username": [1], "email": [2], "active": [3]
Drag options to blanks, or click blank then click option'
A"user123"
B"user@example.com"
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting string values.
Quoting boolean values as strings.
Using invalid boolean literals.