mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-30 01:49:50 +03:00
move limit_choices_to logic from relations.py to utils/field_mapping.py
This commit is contained in:
parent
a66e8ac579
commit
7954c03cbf
|
@ -169,12 +169,6 @@ class RelatedField(Field):
|
|||
# for the field.
|
||||
# Eg: 'MyRelationship(queryset=ExampleModel.objects.all())'
|
||||
queryset = queryset.all()
|
||||
if hasattr(self.parent, "Meta") and hasattr(self.parent.Meta, "model"):
|
||||
filter_dict = self.parent.Meta.model._meta.get_field(
|
||||
self.source
|
||||
).get_limit_choices_to()
|
||||
if filter_dict:
|
||||
queryset = queryset.filter(**filter_dict)
|
||||
return queryset
|
||||
|
||||
def use_pk_only_optimization(self):
|
||||
|
|
|
@ -266,6 +266,9 @@ def get_relation_kwargs(field_name, relation_info):
|
|||
# If this field is read-only, then return early.
|
||||
# No further keyword arguments are valid.
|
||||
return kwargs
|
||||
limit_choices_to = model_field.get_limit_choices_to()
|
||||
if limit_choices_to:
|
||||
kwargs['queryset'] = kwargs['queryset'].filter(**limit_choices_to)
|
||||
|
||||
if model_field.has_default() or model_field.blank or model_field.null:
|
||||
kwargs['required'] = False
|
||||
|
|
Loading…
Reference in New Issue
Block a user