mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-01 11:00:13 +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.
|
# for the field.
|
||||||
# Eg: 'MyRelationship(queryset=ExampleModel.objects.all())'
|
# Eg: 'MyRelationship(queryset=ExampleModel.objects.all())'
|
||||||
queryset = queryset.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
|
return queryset
|
||||||
|
|
||||||
def use_pk_only_optimization(self):
|
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.
|
# If this field is read-only, then return early.
|
||||||
# No further keyword arguments are valid.
|
# No further keyword arguments are valid.
|
||||||
return kwargs
|
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:
|
if model_field.has_default() or model_field.blank or model_field.null:
|
||||||
kwargs['required'] = False
|
kwargs['required'] = False
|
||||||
|
|
Loading…
Reference in New Issue
Block a user