From 5dc605ec0a469748247d9f7e5f009b5584fb4fd7 Mon Sep 17 00:00:00 2001 From: Bruno Alla Date: Mon, 10 Jul 2023 11:40:28 +0100 Subject: [PATCH 1/2] Add FAQ section in README to explain usage of Generics --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 1061d6ff6..b4494be89 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,20 @@ plugins = in your `mypy.ini` file. +## FAQ + +### Model instance is inferred as `Any` instead of my `Model` class + +This library adds support for [Generics](https://peps.python.org/pep-0484/#generics) in DRF. For example, you can now write: + +```python +class MyModelSerializer(serializers.ModelSerializer[MyModel]): + class Meta: + model = MyModel + fields = ("id", "example") +``` + +Which means that methods where the model is being passed around will know the actual type of the model instead of being `Any`. The `instance` attribute on the above serializer will be `Union[MyModel, typing.Sequence[MyModel], None]`. ## To get help From c58b8662fa1faa073e6838a262b5ce5fc201100c Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Wed, 4 Oct 2023 23:20:17 +0300 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b4494be89..85c6df163 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ in your `mypy.ini` file. ### Model instance is inferred as `Any` instead of my `Model` class -This library adds support for [Generics](https://peps.python.org/pep-0484/#generics) in DRF. For example, you can now write: +When subclassing `ModelSerializer`, add a [type argument](https://peps.python.org/pep-0484/#generics) to type-hint the related model class, for example: ```python class MyModelSerializer(serializers.ModelSerializer[MyModel]):