mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-06 21:33:34 +03:00
* Fix issue1811: take limit_choices_to into account with FK * Issue 1811: Add tests to illustrate issue * Filter queryset only if limit_choices_to exists * Move test_relations_with_limited_querysets file within test_relations_pk * move limit_choices_to logic from relations.py to utils/field_mapping.py * move limit_choices_to above other check to avoid conflicts
This commit is contained in:
parent
9c408b296b
commit
e3bd4b9048
|
@ -249,6 +249,10 @@ def get_relation_kwargs(field_name, relation_info):
|
||||||
if to_field:
|
if to_field:
|
||||||
kwargs['to_field'] = to_field
|
kwargs['to_field'] = to_field
|
||||||
|
|
||||||
|
limit_choices_to = model_field and model_field.get_limit_choices_to()
|
||||||
|
if limit_choices_to:
|
||||||
|
kwargs['queryset'] = kwargs['queryset'].filter(**limit_choices_to)
|
||||||
|
|
||||||
if has_through_model:
|
if has_through_model:
|
||||||
kwargs['read_only'] = True
|
kwargs['read_only'] = True
|
||||||
kwargs.pop('queryset', None)
|
kwargs.pop('queryset', None)
|
||||||
|
|
|
@ -52,6 +52,13 @@ class ForeignKeySource(RESTFrameworkModel):
|
||||||
on_delete=models.CASCADE)
|
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
|
# Nullable ForeignKey
|
||||||
class NullableForeignKeySource(RESTFrameworkModel):
|
class NullableForeignKeySource(RESTFrameworkModel):
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
|
|
|
@ -5,10 +5,10 @@ from django.utils import six
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from tests.models import (
|
from tests.models import (
|
||||||
ForeignKeySource, ForeignKeyTarget, ManyToManySource, ManyToManyTarget,
|
ForeignKeySource, ForeignKeySourceWithLimitedChoices, ForeignKeyTarget,
|
||||||
NullableForeignKeySource, NullableOneToOneSource,
|
ManyToManySource, ManyToManyTarget, NullableForeignKeySource,
|
||||||
NullableUUIDForeignKeySource, OneToOnePKSource, OneToOneTarget,
|
NullableOneToOneSource, NullableUUIDForeignKeySource, OneToOnePKSource,
|
||||||
UUIDForeignKeyTarget
|
OneToOneTarget, UUIDForeignKeyTarget
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,6 +38,12 @@ class ForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||||
fields = ('id', 'name', 'target')
|
fields = ('id', 'name', 'target')
|
||||||
|
|
||||||
|
|
||||||
|
class ForeignKeySourceWithLimitedChoicesSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ForeignKeySourceWithLimitedChoices
|
||||||
|
fields = ("id", "target")
|
||||||
|
|
||||||
|
|
||||||
# Nullable ForeignKey
|
# Nullable ForeignKey
|
||||||
class NullableForeignKeySourceSerializer(serializers.ModelSerializer):
|
class NullableForeignKeySourceSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -360,6 +366,18 @@ class PKForeignKeyTests(TestCase):
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
assert 'target' not in serializer.validated_data
|
assert 'target' not in serializer.validated_data
|
||||||
|
|
||||||
|
def test_queryset_size_without_limited_choices(self):
|
||||||
|
limited_target = ForeignKeyTarget(name="limited-target")
|
||||||
|
limited_target.save()
|
||||||
|
queryset = ForeignKeySourceSerializer().fields["target"].get_queryset()
|
||||||
|
assert len(queryset) == 3
|
||||||
|
|
||||||
|
def test_queryset_size_with_limited_choices(self):
|
||||||
|
limited_target = ForeignKeyTarget(name="limited-target")
|
||||||
|
limited_target.save()
|
||||||
|
queryset = ForeignKeySourceWithLimitedChoicesSerializer().fields["target"].get_queryset()
|
||||||
|
assert len(queryset) == 1
|
||||||
|
|
||||||
|
|
||||||
class PKNullableForeignKeyTests(TestCase):
|
class PKNullableForeignKeyTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user