DRF - browsable interface very slow with PrimaryKeyRelatedField

915 views Asked by At

How can I make Django Rest Frameworks browsable UI fast with RelatedField?

I'm aware this has already been asked here: Django REST Framework: slow browsable UI because of large related table but the answer is no longer valid for new versions of DRF

Including two PrimaryKeyRelatedFields gives me a 5s+ load time, removing them takes me back down to under .3

I've tried setting html_cutoff=100 or even html_cutoff=1but it seems to make no difference to load times.

Any ideas? currently on DRF '3.3.2'

Edit: tables involved have 12000 to 120 records - but it would be great to handle much larger amounts

3

There are 3 answers

0
Chozabu On BEST ANSWER

Not quite the answer I am looking for, but currently it looks like there is activity around this already on github - https://github.com/tomchristie/django-rest-framework/issues/3329 with a little luck, one of those patches will get merged soon

1
Lukasz Dynowski On

This question is similar or duplicate of this one Django REST Framework: slow browsable UI because of large related table.

In essence it's N+1 Problem and in context of Django it can be fixed by eager loading of data by calling prefetch_related() or select_related() on QuerySet. Check this answare

0
ziggurat On

Since DRF version 3.4.4, it's possible to limit number of relationships being displayed by using selected fields cutoffs.

From DRF documentations:

When rendered in the browsable API relational fields will default to only displaying a maximum of 1000 selectable items. If more items are present then a disabled option with "More than 1000 items…" will be displayed.

...

You can also control these globally using the settings HTML_SELECT_CUTOFF and HTML_SELECT_CUTOFF_TEXT.