mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-30 01:49:50 +03:00
Issue 1811: Add tests to illustrate issue
This commit is contained in:
parent
2d48dd4e4c
commit
3f95d60a82
|
@ -52,6 +52,13 @@ class ForeignKeySource(RESTFrameworkModel):
|
|||
on_delete=models.CASCADE)
|
||||
|
||||
|
||||
class ForeignKeySourceWithLimitedChoices(RESTFrameworkModel):
|
||||
target = models.ForeignKey(ForeignKeyTarget, help_text='Target',
|
||||
verbose_name='Target',
|
||||
limit_choices_to={"name__startswith": "limited-"},
|
||||
on_delete=models.CASCADE)
|
||||
|
||||
|
||||
# Nullable ForeignKey
|
||||
class NullableForeignKeySource(RESTFrameworkModel):
|
||||
name = models.CharField(max_length=100)
|
||||
|
|
38
tests/test_relations_with_limited_queryset.py
Normal file
38
tests/test_relations_with_limited_queryset.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from django.test import TestCase
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import (
|
||||
ForeignKeySource,
|
||||
ForeignKeyTarget,
|
||||
ForeignKeySourceWithLimitedChoices,
|
||||
)
|
||||
|
||||
|
||||
class ForeignKeySourceWithLimitedChoicesSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ForeignKeySourceWithLimitedChoices
|
||||
fields = ("id", "target")
|
||||
|
||||
|
||||
class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = ForeignKeySource
|
||||
fields = ("id", "target")
|
||||
|
||||
|
||||
class LimitedChoicesInQuerySetTests(TestCase):
|
||||
def setUp(self):
|
||||
for idx in range(1, 4):
|
||||
limited_target = ForeignKeyTarget(name="limited-target-%d" % idx)
|
||||
limited_target.save()
|
||||
target = ForeignKeyTarget(name="target-%d" % idx)
|
||||
target.save()
|
||||
|
||||
def test_queryset_size_without_limited_choices(self):
|
||||
queryset = ForeignKeySourceSerializer().fields["target"].get_queryset()
|
||||
assert len(queryset) == 6
|
||||
|
||||
def test_queryset_size_with_limited_choices(self):
|
||||
queryset = ForeignKeySourceWithLimitedChoicesSerializer().fields["target"].get_queryset()
|
||||
assert len(queryset) == 3
|
Loading…
Reference in New Issue
Block a user