Commit 2d43b17f by Tom Christie Committed by GitHub

Limit queryset when rendering relational choices. (#4375)

Limit querysets to html_cutoff when rendering relational choices
parent 8105a4ac
...@@ -156,14 +156,16 @@ class RelatedField(Field): ...@@ -156,14 +156,16 @@ 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 is not None:
queryset = queryset[:cutoff]
return OrderedDict([ return OrderedDict([
( (
six.text_type(self.to_representation(item)), six.text_type(self.to_representation(item)),
...@@ -173,12 +175,16 @@ class RelatedField(Field): ...@@ -173,12 +175,16 @@ class RelatedField(Field):
]) ])
@property @property
def choices(self):
return self.get_choices()
@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(cutoff=self.html_cutoff),
cutoff=self.html_cutoff, cutoff=self.html_cutoff,
cutoff_text=self.html_cutoff_text cutoff_text=self.html_cutoff_text
) )
...@@ -487,9 +493,12 @@ class ManyRelatedField(Field): ...@@ -487,9 +493,12 @@ class ManyRelatedField(Field):
for value in iterable for value in iterable
] ]
def get_choices(self, cutoff=None):
return self.child_relation.get_choices(cutoff)
@property @property
def choices(self): def choices(self):
return self.child_relation.choices return self.get_choices()
@property @property
def grouped_choices(self): def grouped_choices(self):
...@@ -497,7 +506,7 @@ class ManyRelatedField(Field): ...@@ -497,7 +506,7 @@ class ManyRelatedField(Field):
def iter_options(self): def iter_options(self):
return iter_options( return iter_options(
self.grouped_choices, self.get_choices(cutoff=self.html_cutoff),
cutoff=self.html_cutoff, cutoff=self.html_cutoff,
cutoff_text=self.html_cutoff_text cutoff_text=self.html_cutoff_text
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment