Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango ManyToMany Template rendering and performance issues
    primarykey
    data
    text
    <p>I've got a django model that contains a manytomany relationship, of the type,</p> <pre><code>class MyModel(models.Model): name = .. refby = models.ManyToManyField(MyModel2) .. class MyModel2(..): name = .. date = .. </code></pre> <p>I need to render it in my template such that I am able to render all the mymodel2 objects that refer to mymodel. Currently, I do something like the following,</p> <pre><code>{% for i in mymodel_obj_list %} {{i.name}} {% for m in i.refby.all|dictsortreversed:"date"|slice:"3" %} {{.. }} {% endfor %} &lt;div&gt; &lt;!--This div toggles hidden/visible, shows next 12--&gt; {% for n in i.refby.all|dictsortreversed:"date"|slice:"3:15" %} {{.. }} {% endfor %} &lt;/div&gt; {% endfor %} </code></pre> <p>As the code suggests, I only want to show the latest 3 mymodel2 objects, sorted in reverse order by date, although the next 12 do get loaded.</p> <p>Is this a very inefficient method of doing so? (Given that results for the refby.all could be a few 100s, and the total no of results in "mymodel_obj_list" is also in 100s - I use a paginator there).</p> <p>In which case, whats the best method to pre-compute these refby's and render them to the template? Should I do the sorting and computation in the view, and then pass it? I wasn't sure how to do this in order to maintain my pagination.</p> <p>View code looks something like,</p> <pre><code>obj_list = Table.objects.filter(..) # Few 100 records pl = CustomPaginatorClass(obj_list...) </code></pre> <p>And I pass the pl to the page as mymodel_obj_list.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload