Complete the code to make the 'description' field optional in a FastAPI model.
from typing import [1] from pydantic import BaseModel class Item(BaseModel): name: str description: [1][str] = None
Using Optional from typing allows the field to be optional, meaning it can be None or a string.
Complete the code to allow the 'price' field to be nullable (accept None) in a FastAPI model.
from typing import Optional from pydantic import BaseModel class Product(BaseModel): name: str price: Optional[[1]] = None
The price field should be a float or None, so we use Optional[float].
Fix the error in the code to correctly declare an optional nullable field 'tags' as a list of strings.
from typing import [1], List from pydantic import BaseModel class BlogPost(BaseModel): title: str tags: [1][List[str]] = None
To make 'tags' optional and nullable, wrap the list type with Optional.
Fill both blanks to define a FastAPI model with an optional nullable 'rating' field of type float and a required 'title' field.
from typing import [1] from pydantic import BaseModel class Review(BaseModel): title: str rating: [2][float] = None
Both blanks use Optional to make the 'rating' field optional and nullable.
Fill all three blanks to create a FastAPI model with an optional nullable 'email' field of type string, a required 'username' field, and an optional nullable 'age' field of type int.
from typing import [1] from pydantic import BaseModel class User(BaseModel): username: str email: [2][str] = None age: [3][int] = None
Use Optional for all optional nullable fields. Here, email and age are optional and nullable.