I'm using django rest framework version 3.3.2.
We use HyperlinkedRelatedField
in hundreds of different places, and my problem is that this inherits a choices
method through RelatedField
which does the following:
class RelatedField(Field):
...
@property
def choices(self):
queryset = self.get_queryset()
if queryset is None:
# Ensure that field.choices returns something sensible
# even when accessed with a read-only field.
return {}
return OrderedDict([
(
six.text_type(self.to_representation(item)),
self.display_value(item)
)
for item in queryset
])
That queryset is a relation to another table, and can contains hundreds of thousands of rows. An OPTIONS request to the api now consumes all available memory, as it tries to generate the json response for the available choices of the relation. Even though html_cutoff
option truncates this number to 1000, the issue remains because the queryset has already been consumed before it is limited by the cutoff.
I'm looking for a non-intrusive way to disable the choices enumeration on foreign keys. I would like to avoid creating a custom field class, if possible, is there a way to influence this behaviour through the rest framework api? I don't need to see the choices
at all in the options response.