Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace update_forward_refs with model_rebuild in docs #1366

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/docs/guides/response/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,15 @@ Sometimes you need to create a schema that has reference to itself, or tree-stru
To do that you need:

- set a type of your schema in quotes
- use `update_forward_refs` method to apply self referencing types
- use `model_rebuild` method to apply self referencing types

```python hl_lines="3 6"
class Organization(Schema):
title: str
part_of: 'Organization' = None #!! note the type in quotes here !!


Organization.update_forward_refs() # !!! this is important
Organization.model_rebuild() # !!! this is important


@api.get('/organizations', response=List[Organization])
Expand All @@ -378,20 +378,20 @@ def list_organizations(request):

## Self-referencing schemes from `create_schema()`

To be able to use the method `update_forward_refs()` from a schema generated via `create_schema()`,
To be able to use the method `model_rebuild()` from a schema generated via `create_schema()`,
the "name" of the class needs to be in our namespace. In this case it is very important to pass
the `name` parameter to `create_schema()`

```python hl_lines="3"
UserSchema = create_schema(
User,
name='UserSchema', # !!! this is important for update_forward_refs()
name='UserSchema', # !!! this is important for model_rebuild()
fields=['id', 'username']
custom_fields=[
('manager', 'UserSchema', None),
]
)
UserSchema.update_forward_refs()
UserSchema.model_rebuild()
```

## Serializing Outside of Views
Expand Down
Loading