Skip to content

Allowed Q objects in limit_choices_to introspection. #6472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2019
Merged
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
4 changes: 3 additions & 1 deletion rest_framework/utils/field_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ def get_relation_kwargs(field_name, relation_info):

limit_choices_to = model_field and model_field.get_limit_choices_to()
if limit_choices_to:
kwargs['queryset'] = kwargs['queryset'].filter(**limit_choices_to)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carltongibson cool new feature. A question: it support use a callable like django ForeignKey.limit_choices_to?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
kwargs['read_only'] = True
Expand Down
7 changes: 7 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class ForeignKeySourceWithLimitedChoices(RESTFrameworkModel):
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
class NullableForeignKeySource(RESTFrameworkModel):
name = models.CharField(max_length=100)
Expand Down
21 changes: 17 additions & 4 deletions tests/test_relations_pk.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

from rest_framework import serializers
from tests.models import (
ForeignKeySource, ForeignKeySourceWithLimitedChoices, ForeignKeyTarget,
ManyToManySource, ManyToManyTarget, NullableForeignKeySource,
NullableOneToOneSource, NullableUUIDForeignKeySource, OneToOnePKSource,
OneToOneTarget, UUIDForeignKeyTarget
ForeignKeySource, ForeignKeySourceWithLimitedChoices,
ForeignKeySourceWithQLimitedChoices, ForeignKeyTarget, ManyToManySource,
ManyToManyTarget, NullableForeignKeySource, NullableOneToOneSource,
NullableUUIDForeignKeySource, OneToOnePKSource, OneToOneTarget,
UUIDForeignKeyTarget
)


Expand Down Expand Up @@ -378,6 +379,18 @@ def test_queryset_size_with_limited_choices(self):
queryset = ForeignKeySourceWithLimitedChoicesSerializer().fields["target"].get_queryset()
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):
def setUp(self):
Expand Down