mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-28 17:33:15 +03:00
Allowed Q objects in limit_choices_to introspection. (#6472)
Closes #6470.
This commit is contained in:
parent
07c5c968ce
commit
8a29c53226
|
@ -251,7 +251,9 @@ def get_relation_kwargs(field_name, relation_info):
|
||||||
|
|
||||||
limit_choices_to = model_field and model_field.get_limit_choices_to()
|
limit_choices_to = model_field and model_field.get_limit_choices_to()
|
||||||
if limit_choices_to:
|
if limit_choices_to:
|
||||||
kwargs['queryset'] = kwargs['queryset'].filter(**limit_choices_to)
|
if not isinstance(limit_choices_to, models.Q):
|
||||||
|
limit_choices_to = models.Q(**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
|
||||||
|
|
|
@ -59,6 +59,13 @@ class ForeignKeySourceWithLimitedChoices(RESTFrameworkModel):
|
||||||
on_delete=models.CASCADE)
|
on_delete=models.CASCADE)
|
||||||
|
|
||||||
|
|
||||||
|
class ForeignKeySourceWithQLimitedChoices(RESTFrameworkModel):
|
||||||
|
target = models.ForeignKey(ForeignKeyTarget, help_text='Target',
|
||||||
|
verbose_name='Target',
|
||||||
|
limit_choices_to=models.Q(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,11 @@ from django.utils import six
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from tests.models import (
|
from tests.models import (
|
||||||
ForeignKeySource, ForeignKeySourceWithLimitedChoices, ForeignKeyTarget,
|
ForeignKeySource, ForeignKeySourceWithLimitedChoices,
|
||||||
ManyToManySource, ManyToManyTarget, NullableForeignKeySource,
|
ForeignKeySourceWithQLimitedChoices, ForeignKeyTarget, ManyToManySource,
|
||||||
NullableOneToOneSource, NullableUUIDForeignKeySource, OneToOnePKSource,
|
ManyToManyTarget, NullableForeignKeySource, NullableOneToOneSource,
|
||||||
OneToOneTarget, UUIDForeignKeyTarget
|
NullableUUIDForeignKeySource, OneToOnePKSource, OneToOneTarget,
|
||||||
|
UUIDForeignKeyTarget
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -378,6 +379,18 @@ class PKForeignKeyTests(TestCase):
|
||||||
queryset = ForeignKeySourceWithLimitedChoicesSerializer().fields["target"].get_queryset()
|
queryset = ForeignKeySourceWithLimitedChoicesSerializer().fields["target"].get_queryset()
|
||||||
assert len(queryset) == 1
|
assert len(queryset) == 1
|
||||||
|
|
||||||
|
def test_queryset_size_with_Q_limited_choices(self):
|
||||||
|
limited_target = ForeignKeyTarget(name="limited-target")
|
||||||
|
limited_target.save()
|
||||||
|
|
||||||
|
class QLimitedChoicesSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ForeignKeySourceWithQLimitedChoices
|
||||||
|
fields = ("id", "target")
|
||||||
|
|
||||||
|
queryset = QLimitedChoicesSerializer().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