1

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

Community
  • 1
  • 1
Chozabu
  • 1,015
  • 1
  • 10
  • 33
  • Might as well confirm it: is it still slow when you are retrieving it as JSON or does this only affect the browsable API? – Kevin Brown-Silva Feb 07 '16 at 15:57
  • Plain JSON is fine. Sorry, I should have said that in the questions. See my "answer" below - it is a known issue, with a couple of PRs that could solve it in different ways – Chozabu Feb 07 '16 at 17:16

3 Answers3

2

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.

Community
  • 1
  • 1
ziggurat
  • 92
  • 7
1

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

Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124
  • Hey, thanks for answering this old question. I actually link to that question in my question. I've marked my answer as correct, and commented a link to the PR that fixed my issue, but upvoted your answer. – Chozabu Mar 28 '19 at 02:05
0

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

Chozabu
  • 1,015
  • 1
  • 10
  • 33