0
0
FastAPIframework~10 mins

Field types and constraints 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 declare a string field with a maximum length of 50 characters.

FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    name: str = Field(max_length=[1])
Drag options to blanks, or click blank then click option'
A50
B100
C0
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using min_length instead of max_length
Setting length to 0 which means no characters allowed
2fill in blank
medium

Complete the code to declare an integer field that must be greater than or equal to 18.

FastAPI
from pydantic import BaseModel, Field

class User(BaseModel):
    age: int = Field(ge=[1])
Drag options to blanks, or click blank then click option'
A1
B0
C21
D18
Attempts:
3 left
💡 Hint
Common Mistakes
Using gt instead of ge
Setting the value too low like 0
3fill in blank
hard

Fix the error in the code by completing the field declaration to require a string with a minimum length of 3.

FastAPI
from pydantic import BaseModel, Field

class Product(BaseModel):
    code: str = Field(min_length=[1])
Drag options to blanks, or click blank then click option'
A1
B0
C3
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using max_length instead of min_length
Setting the value too low like 0 or 1
4fill in blank
hard

Fill both blanks to declare a float field that must be between 0.0 and 100.0 inclusive.

FastAPI
from pydantic import BaseModel, Field

class Score(BaseModel):
    value: float = Field(ge=[1], le=[2])
Drag options to blanks, or click blank then click option'
A0.0
B100.0
C50.0
D1.0
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up ge and le
Using integer values instead of floats
5fill in blank
hard

Fill both blanks to create a string field that is required, has a minimum length of 5, and a maximum length of 20.

FastAPI
from pydantic import BaseModel, Field

class Account(BaseModel):
    username: str = Field(..., min_length=[1], max_length=[2])
Drag options to blanks, or click blank then click option'
Arequired=True
B5
C20
Dnullable=False
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid arguments like required=True or nullable=False
Setting the length values too low like 0