Skip to content

Commit 2d43b17

Browse files
authored
Limit queryset when rendering relational choices. (#4375)
Limit querysets to html_cutoff when rendering relational choices
1 parent 8105a4a commit 2d43b17

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

rest_framework/relations.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,16 @@ def get_attribute(self, instance):
156156
# Standard case, return the object instance.
157157
return get_attribute(instance, self.source_attrs)
158158

159-
@property
160-
def choices(self):
159+
def get_choices(self, cutoff=None):
161160
queryset = self.get_queryset()
162161
if queryset is None:
163162
# Ensure that field.choices returns something sensible
164163
# even when accessed with a read-only field.
165164
return {}
166165

166+
if cutoff is not None:
167+
queryset = queryset[:cutoff]
168+
167169
return OrderedDict([
168170
(
169171
six.text_type(self.to_representation(item)),
@@ -172,13 +174,17 @@ def choices(self):
172174
for item in queryset
173175
])
174176

177+
@property
178+
def choices(self):
179+
return self.get_choices()
180+
175181
@property
176182
def grouped_choices(self):
177183
return self.choices
178184

179185
def iter_options(self):
180186
return iter_options(
181-
self.grouped_choices,
187+
self.get_choices(cutoff=self.html_cutoff),
182188
cutoff=self.html_cutoff,
183189
cutoff_text=self.html_cutoff_text
184190
)
@@ -487,17 +493,20 @@ def to_representation(self, iterable):
487493
for value in iterable
488494
]
489495

496+
def get_choices(self, cutoff=None):
497+
return self.child_relation.get_choices(cutoff)
498+
490499
@property
491500
def choices(self):
492-
return self.child_relation.choices
501+
return self.get_choices()
493502

494503
@property
495504
def grouped_choices(self):
496505
return self.choices
497506

498507
def iter_options(self):
499508
return iter_options(
500-
self.grouped_choices,
509+
self.get_choices(cutoff=self.html_cutoff),
501510
cutoff=self.html_cutoff,
502511
cutoff_text=self.html_cutoff_text
503512
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy