Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Unreleased
* Make serializer classes generic (@noamkush)

## 0.7.2
* Add support for Django 5.1, 5.2 and Python 3.13 (@browniebroke)
* Drop support for end-of-life Python 3.8 (@browniebroke)
Expand Down
14 changes: 8 additions & 6 deletions drf_writable_nested/mixins.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# -*- coding: utf-8 -*-
from collections import OrderedDict, defaultdict
from typing import List, Tuple
from typing import List, Tuple, TypeVar

from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import FieldDoesNotExist
from django.db.models import ProtectedError, SET_NULL, SET_DEFAULT
from django.db.models import ProtectedError, SET_NULL, SET_DEFAULT, Model
from django.db.models.fields.related import ForeignObjectRel, ManyToManyRel
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.validators import UniqueValidator

_MT = TypeVar("_MT", bound=Model)

class BaseNestedModelSerializer(serializers.ModelSerializer):

class BaseNestedModelSerializer(serializers.ModelSerializer[_MT]):
def _extract_relations(self, validated_data):
reverse_relations = OrderedDict()
relations = OrderedDict()
Expand Down Expand Up @@ -241,7 +243,7 @@ def _get_save_kwargs(self, field_name):
return save_kwargs


class NestedCreateMixin(BaseNestedModelSerializer):
class NestedCreateMixin(BaseNestedModelSerializer[_MT]):
"""
Adds nested create feature
"""
Expand All @@ -262,7 +264,7 @@ def create(self, validated_data):
return instance


class NestedUpdateMixin(BaseNestedModelSerializer):
class NestedUpdateMixin(BaseNestedModelSerializer[_MT]):
"""
Adds update nested feature
"""
Expand Down Expand Up @@ -362,7 +364,7 @@ def delete_reverse_relations_if_need(self, instance, reverse_relations):
str(instance) for instance in instances]))


class UniqueFieldsMixin(serializers.ModelSerializer):
class UniqueFieldsMixin(serializers.ModelSerializer[_MT]):
"""
Moves `UniqueValidator`'s from the validation stage to the save stage.
It solves the problem with nested validation for unique fields on update.
Expand Down
8 changes: 6 additions & 2 deletions drf_writable_nested/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from typing import TypeVar

from django.db.models import Model
from rest_framework import serializers

from .mixins import NestedCreateMixin, NestedUpdateMixin

_MT = TypeVar("_MT", bound=Model)

class WritableNestedModelSerializer(NestedCreateMixin, NestedUpdateMixin,
serializers.ModelSerializer):
class WritableNestedModelSerializer(NestedCreateMixin[_MT], NestedUpdateMixin[_MT],
serializers.ModelSerializer[_MT]):
pass
5 changes: 4 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ plugins =
django_settings_module = "example.settings"

[mypy-setuptools.*]
ignore_missing_imports = True
ignore_missing_imports = True

[mypy-drf_writable_nested.*]
disallow_any_generics = True