Skip to content

Commit

Permalink
Add serializer Doc
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRn76 committed Jan 23, 2024
1 parent 943e7e7 commit 84139e6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/docs/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 3.7.0
- Add `ModelSerializer`

### 3.6.0
- Use `observable` pattern for loading database middleware and inheritance of the `Query` class
- Remove `IDType` from the `Model`
Expand Down
72 changes: 72 additions & 0 deletions docs/docs/serializer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
You can write your `serializer` in 2 style:


## Style 1 (Pydantic)
Write a normal `pydantic` class and use it as serializer:

```python
from pydantic import BaseModel
from pydantic import Field

from panther.app import API
from panther.request import Request
from panther.response import Response


class UserSerializer(BaseModel):
username: str
password: str
first_name: str = Field(default='', min_length=2)
last_name: str = Field(default='', min_length=4)


@API(input_model=UserSerializer)
async def serializer_example(request: Request):
return Response(data=request.validated_data)
```

## Style 2 (Model Serializer)
Use panther `ModelSerializer` to write your serializer which will use your `model` fields as its fields, and you can say which fields are `required`

```python
from pydantic import Field

from panther import status
from panther.app import API
from panther.db import Model
from panther.request import Request
from panther.response import Response
from panther.serializer import ModelSerializer


class User(Model):
username: str
password: str
first_name: str = Field(default='', min_length=2)
last_name: str = Field(default='', min_length=4)


class UserModelSerializer(metaclass=ModelSerializer, model=User):
fields = ['username', 'first_name', 'last_name']
required_fields = ['first_name']


@API(input_model=UserModelSerializer)
async def model_serializer_example(request: Request):
return Response(data=request.validated_data, status_code=status.HTTP_202_ACCEPTED)
```

### Notes:
1. In the example above `UserModelSerializer` only accepts the values of `fields` attribute

2. In default the `UserModelSerializer.fields` are same as `User.fields` but you can change their default and make them required with `required_fields` attribute

3. If you want uses `required_fields` you have to put them in `fields` too.

4. `fields` attribute is `required` when you are using `ModelSerializer` as `metaclass`

5. `model=` is required when you are using `ModelSerializer` as `metaclass`

6. You have to use `ModelSerializer` as `metaclass` (not as a parent)

7. Panther is going to create a `pydantic` model as your `UserModelSerializer` in the startup
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ nav:
- Working With Database: 'working_with_db.md'
- Panther ODM: 'panther_odm.md'
- Configs: 'configs.md'
- Serializer: 'serializer.md'
- WebSocket: 'websocket.md'
- Monitoring: 'monitoring.md'
- Log Queries: 'log_queries.md'
Expand Down
2 changes: 1 addition & 1 deletion panther/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from panther.main import Panther # noqa: F401

__version__ = '3.6.0'
__version__ = '3.7.0'


def version():
Expand Down

0 comments on commit 84139e6

Please sign in to comment.