Country  ](../../api/pydantic_extra_types_country/)
      * [ Payment  ](../../api/pydantic_extra_types_payment/)
      * [ Phone Numbers  ](../../api/pydantic_extra_types_phone_numbers/)
      * [ Routing Numbers  ](../../api/pydantic_extra_types_routing_numbers/)
      * [ Coordinate  ](../../api/pydantic_extra_types_coordinate/)
      * [ Mac Address  ](../../api/pydantic_extra_types_mac_address/)
      * [ ISBN  ](../../api/pydantic_extra_types_isbn/)
      * [ Pendulum  ](../../api/pydantic_extra_types_pendulum_dt/)
      * [ Currency  ](../../api/pydantic_extra_types_currency_code/)
      * [ Language  ](../../api/pydantic_extra_types_language_code/)
  * Examples  Examples 
    * [ Secrets  ](../../examples/secrets/)
  * Error Messages  Error Messages 
    * [ Error Handling  ](../../errors/errors/)
    * [ Validation Errors  ](../../errors/validation_errors/)
    * [ Usage Errors  ](../../errors/usage_errors/)
  * Integrations  Integrations 
    * [ Mypy  ](../../integrations/mypy/)
    * [ PyCharm  ](../../integrations/pycharm/)
    * [ Hypothesis  ](../../integrations/hypothesis/)
    * [ Visual Studio Code  ](../../integrations/visual_studio_code/)
    * [ datamodel-code-generator  ](../../integrations/datamodel_code_generator/)
    * [ devtools  ](../../integrations/devtools/)
    * [ Rich  ](../../integrations/rich/)
    * [ Linting  ](../../integrations/linting/)
  * [ Blog  ](https://blog.pydantic.dev/)
  * [ Pydantic People  ](../../pydantic_people/)

Page contents

  * Default values 
  * Using Annotated 
  * Field aliases 
    * Validation Alias 
  * Numeric Constraints 
  * String Constraints 
  * Decimal Constraints 
  * Dataclass Constraints 
  * Validate Default Values 
  * Field Representation 
  * Discriminator 
  * Strict Mode 
  * Immutability 
  * Exclude 
  * Deprecated fields 
    * deprecated as a string 
    * deprecated via the warnings.deprecated decorator 
    * deprecated as a boolean 
  * Customizing JSON Schema 
  * The computed_field decorator 

#  Fields

API Documentation

[ ` pydantic.fields.Field ` ](../../api/fields/#pydantic.fields.Field)  

The [ ` Field ` ](../../api/fields/#pydantic.fields.Field) function is used to
customize and add metadata to fields of models.

##  Default values  ¶

The ` default ` parameter is used to define a default value for a field.

    
    
    from pydantic import BaseModel, Field
    
    
    class User(BaseModel):
        name: str = Field(default='John Doe')
    
    
    user = User()
    print(user)
    #> name='John Doe'
    

You can also use ` default_factory ` to define a callable that will be called
to generate a default value.

    
    
    from uuid import uuid4
    
    from pydantic import BaseModel, Field
    
    
    class User(BaseModel):
        id: str = Field(default_factory=lambda: uuid4().hex)
    

Info

The ` default ` and ` default_factory ` parameters are mutually exclusive.

Note

If you use ` typing.Optional ` , it doesn't mean that the field has a default
value of ` None ` !

##  Using ` Annotated ` ¶

The [ ` Field ` ](../../api/fields/#pydantic.fields.Field) function can also
be used together with [ ` Annotated `
](https://docs.python.org/3/library/typing.html#typing.Annotated) .

    
    
    from uuid import uuid4
    
    from typing_extensions import Annotated
    
    from pydantic import BaseModel, Field
    
    
    class User(BaseModel):
        id: Annotated[str, Field(default_factory=lambda: uuid4().hex)]
    

Note

Defaults can be set outside [ ` Annotated `
](https://docs.python.org/3/library/typing.html#typing.Annotated) as the
assigned value or with ` Field.default_factory ` inside [ ` Annotated `
](https://docs.python.org/3/library/typing.html#typing.Annotated) . The `
Field.default ` argument is not supported inside [ ` Annotated `
](https://docs.python.org/3/library/typing.html#typing.Annotated) .

##  Field aliases  ¶

For validation and serialization, you can define an alias for a field.

There a