From 0d46e5866399f2a721487595f0a9df305d07ff7d Mon Sep 17 00:00:00 2001 From: 954 <510485871@qq.com> Date: Tue, 22 Nov 2022 12:47:22 +0800 Subject: [PATCH] Commit unit test --- tests/models.py | 26 ++++++++++++++++++++ tests/test_serializer_lists.py | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/tests/models.py b/tests/models.py index 666e9f00316..88e3d8dcaf3 100644 --- a/tests/models.py +++ b/tests/models.py @@ -2,6 +2,8 @@ from django.contrib.auth.models import User from django.db import models +from django.db.models import QuerySet +from django.db.models.manager import BaseManager from django.utils.translation import gettext_lazy as _ @@ -124,3 +126,27 @@ class OneToOnePKSource(RESTFrameworkModel): target = models.OneToOneField( OneToOneTarget, primary_key=True, related_name='required_source', on_delete=models.CASCADE) + + +class CustomManagerModel(RESTFrameworkModel): + class CustomManager: + def __new__(cls, *args, **kwargs): + cls = BaseManager.from_queryset( + QuerySet + ) + return cls + + objects = CustomManager()() + # `CustomManager()` will return a `BaseManager` class. + # We need to instantiation it, so we write `CustomManager()()` here. + + text = models.CharField( + max_length=100, + verbose_name=_("Text comes here"), + help_text=_("Text description.") + ) + + o2o_target = models.ForeignKey(OneToOneTarget, + help_text='OneToOneTarget', + verbose_name='OneToOneTarget', + on_delete=models.CASCADE) diff --git a/tests/test_serializer_lists.py b/tests/test_serializer_lists.py index 551f6266622..661799fae4b 100644 --- a/tests/test_serializer_lists.py +++ b/tests/test_serializer_lists.py @@ -6,6 +6,11 @@ from rest_framework import serializers from rest_framework.exceptions import ErrorDetail +from tests.models import ( + CustomManagerModel, + NullableOneToOneSource, + OneToOneTarget +) class BasicObject: @@ -683,3 +688,43 @@ def test_min_max_length_six_items(self): assert min_serializer.validated_data == input_data assert not max_min_serializer.is_valid() + + +@pytest.mark.django_db() +class TestToRepresentationManagerCheck: + """ + https://github.com/encode/django-rest-framework/issues/8726 + """ + + def setup(self): + class CustomManagerModelSerializer(serializers.ModelSerializer): + class Meta: + model = CustomManagerModel + fields = '__all__' + + class OneToOneTargetSerializer(serializers.ModelSerializer): + my_model = CustomManagerModelSerializer(many=True, source="custommanagermodel_set") + + class Meta: + model = OneToOneTarget + fields = '__all__' + depth = 3 + + class NullableOneToOneSourceSerializer(serializers.ModelSerializer): + target = OneToOneTargetSerializer() + + class Meta: + model = NullableOneToOneSource + fields = '__all__' + + self.serializer = NullableOneToOneSourceSerializer + + def test(self): + o2o_target = OneToOneTarget.objects.create(name='OneToOneTarget') + NullableOneToOneSource.objects.create( + name='NullableOneToOneSource', + target=o2o_target + ) + queryset = NullableOneToOneSource.objects.all() + serializer = self.serializer(queryset, many=True) + assert serializer.data