Skip to content

Limits the number of related items added to the choices dictionary on RelatedField #3330

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

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 9 additions & 3 deletions rest_framework/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ def get_attribute(self, instance):
# Standard case, return the object instance.
return get_attribute(instance, self.source_attrs)

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

if cutoff:
queryset = queryset[:cutoff]

return OrderedDict([
(
six.text_type(self.to_representation(item)),
Expand All @@ -157,13 +159,17 @@ def choices(self):
for item in queryset
])

@property
def choices(self):
return self.get_choices()

@property
def grouped_choices(self):
return self.choices

def iter_options(self):
return iter_options(
self.grouped_choices,
self.get_choices(self.html_cutoff),
cutoff=self.html_cutoff,
cutoff_text=self.html_cutoff_text
)
Expand Down
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