'How can I implement field level decorators in Python?

One of the things I like about Java/Spring is the support for field level decorators for MongoDB. Can something similar to what's below be achieved in Python (preferably using Pydantic)?

class Customer(BaseModel):
  id: PyObjectId = Field(alias='_id')
  first_name: str = Field(alias='firstName')
  last_name: str = Field(alias='lastName')
  @Transient
  full_name: Optional[str] = Field(alias='fullName')

By marking a field as @Transient, it would not be persisted to MongoDB but would be included when the object is encoded to JSON in a RESTful API.

Perhaps there are better ways to achieve this but it seems like a good use case for decorators.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source