mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 14:24:48 +03:00
Keep the same RelatedField.choices behaviour while allowing iter_options to limit the amount of choices
This commit is contained in:
parent
cb29f1ab5e
commit
66ade04b14
|
@ -141,29 +141,35 @@ class RelatedField(Field):
|
||||||
# Standard case, return the object instance.
|
# Standard case, return the object instance.
|
||||||
return get_attribute(instance, self.source_attrs)
|
return get_attribute(instance, self.source_attrs)
|
||||||
|
|
||||||
@property
|
def get_choices(self, cutoff=None):
|
||||||
def choices(self):
|
|
||||||
queryset = self.get_queryset()
|
queryset = self.get_queryset()
|
||||||
if queryset is None:
|
if queryset is None:
|
||||||
# Ensure that field.choices returns something sensible
|
# Ensure that field.choices returns something sensible
|
||||||
# even when accessed with a read-only field.
|
# even when accessed with a read-only field.
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
if cutoff:
|
||||||
|
queryset = queryset[:cutoff]
|
||||||
|
|
||||||
return OrderedDict([
|
return OrderedDict([
|
||||||
(
|
(
|
||||||
six.text_type(self.to_representation(item)),
|
six.text_type(self.to_representation(item)),
|
||||||
self.display_value(item)
|
self.display_value(item)
|
||||||
)
|
)
|
||||||
for item in queryset[:self.html_cutoff]
|
for item in queryset
|
||||||
])
|
])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def choices(self):
|
||||||
|
return self.get_choices()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def grouped_choices(self):
|
def grouped_choices(self):
|
||||||
return self.choices
|
return self.choices
|
||||||
|
|
||||||
def iter_options(self):
|
def iter_options(self):
|
||||||
return iter_options(
|
return iter_options(
|
||||||
self.grouped_choices,
|
self.get_choices(self.html_cutoff),
|
||||||
cutoff=self.html_cutoff,
|
cutoff=self.html_cutoff,
|
||||||
cutoff_text=self.html_cutoff_text
|
cutoff_text=self.html_cutoff_text
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user