Saturday, February 12, 2022

Re: Formsets?!?!?!?!?!?!?!?!?!?

Hello,


I have also seen some discussions about formsets recently. I think it's not as simple as that. I don't believe there's any rule that applies to all cases. Each case is probably different.


You say you have 100 rows with 12 fields each, and that it takes 19 seconds to render. Where are these 19 seconds spent? It could be that the data is fetched from the database instantly and that the 19 seconds are spent in order for some template to render. Or it could be that even this is done quite fast and that the resulting html is too complicated and the browser takes too long to render it. Or something else.


While I don't have much experience with formsets, I've made some pretty complicated stuff in the admin (which makes heavy use of formsets). I once had a database of scientific publications, and each publication could have an indefinite number of authors. If a publication had, say, 10 authors, then the author formset would repeat itself 10 times (plus two or three more empty ones for any additional you'd want to register). Each of these 12-13 formsets had an "author" dropdown field. There were hundreds of authors in the database, so each of the 12-13 dropdowns had hundreds of options. Imagine this:


  <select name="author">

    <option value="26">Antonis Christofides</option>

    <option value="42">Steve Smith</option>


    [... 300 more options here]


  </select>


all this repeated 13 times. There wasn't that anything in there that was particularly slow, it was the sheer volume of information that made it slow. In that case the solution was obvious—I just needed to use the admin's raw_id_fields option.


So there's no generic way to diagnose the problem. You need to investigate. Start with the Django Debug Toolbar and check where this time is spent. Then try reducing the code gradually to arrive at a minimal example. When you have identified what causes the delay, you will be able either to solve it yourself or to ask us a more specific question. (Either way tell us what happened.)


Regards,


Antonis

Antonis Christofides  +30-6979924665 (mobile)


On 12/02/2022 07.38, Steve Smith wrote:
Hello all....

I have formsets working....but I seem to be encountering some performance issues if I have more than 25 rows.  I've looked at my database calls....select_related...prefetch_related...and I've polled several audiences...SO....Facebook Groups....and the consensus seems to be that Formsets are bad if you have a lot of rows.  I'm having trouble accepting that because if that was the case why do they exist?  And why is the default like 1000 forms?  I really like the formset concept....but I'm having a hard time accepting that if you have more than 25 rows it's just gonna be "slow".  And by slow I mean when I try to render the form using formsets and I have 100 rows of data with like 12 fields each...it takes 19 seconds or so for the form to render.  Does this sound like normal behavior?  I'm legitimately asking as I honestly don't know.

Thanks in advance for any thoughts or feedback.

Steve

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CH0PR18MB4292692992A37BD51AAEB0AFD2319%40CH0PR18MB4292.namprd18.prod.outlook.com.

No comments:

Post a Comment