Skip to content

Commit

Permalink
Merge pull request #1366 from Danie-1/master
Browse files Browse the repository at this point in the history
Replace update_forward_refs with model_rebuild in docs
  • Loading branch information
vitalik authored Dec 19, 2024
2 parents 96ba386 + d2e17e3 commit be55f63
Showing 1 changed file with 5 additions and 5 deletions.
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

0 comments on commit be55f63

Please sign in to comment.