Skip to content

What is the best way to create a read-only/allow_mutation=False field? #110

Open
@jidn

Description

@jidn

Similar to the id field, I have a created_on field the database will auto create for me. Now, what is the best way to not allow the field in POST, PUT but have it in GETs?

  1. Require the use of Create and Update Schema. Nothing would need to change, but all that schema creation smells of boilerplate.
  2. Modify the _utils.py schema_factory to allow for additional fields to exclude. However, at that point you might as well add id to that list and make it generic.
  3. Modify the _utils.py schema_factory to look for allow_mutation and exclude it along with id.
for f in schema_cls.__fields__.values()
if f.name != pk_field_name and f.field_info.allow_mutation

Now my field could look something like

# Not in OpenAPI post or put
update_on: datetime.datetime = Field(
    allow_mutation=False, description="Last update from sync system"
)

Is there a better way to remove fields from Create and Update schemas easily?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions