From cb29f1ab5e900f18db617012178b8c845fdb6f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rube=CC=81n=20Dura=CC=81=20Tari=CC=81?= Date: Tue, 25 Aug 2015 16:37:14 +0100 Subject: [PATCH 1/2] Limits the number of related items added to the choices dictionary on RelatedField --- rest_framework/relations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 56af657d9d..8fd50edddb 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -154,7 +154,7 @@ def choices(self): six.text_type(self.to_representation(item)), self.display_value(item) ) - for item in queryset + for item in queryset[:self.html_cutoff] ]) @property From 66ade04b145a28622b1e78d04e6e99e7726fc20e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rube=CC=81n=20Dura=CC=81=20Tari=CC=81?= Date: Tue, 25 Aug 2015 17:47:00 +0100 Subject: [PATCH 2/2] Keep the same RelatedField.choices behaviour while allowing iter_options to limit the amount of choices --- rest_framework/relations.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 8fd50edddb..3d3314f586 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -141,29 +141,35 @@ 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)), self.display_value(item) ) - for item in queryset[:self.html_cutoff] + 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 ) 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