Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you want to use is <a href="http://docs.python.org/2/library/itertools.html#itertools.chain.from_iterable" rel="nofollow">chain.from_iterable</a>. This will prevent the queries from being run more than once.</p> <pre><code>gallery_list = tuple(chain.from_iterable(grade_pk_categories,grade_pk_galleries,grade_k_categories,grade_k_galleries,grade_1_categories,grade_1_galleries,grade_2_categories,grade_2_galleries,grade_3_categories,grade_3_galleries,grade_4_categories,grade_4_galleries,grade_5_categories,grade_5_galleries,grade_6_categories,grade_6_galleries,grade_7_categories,grade_7_galleries,grade_8_categories,grade_8_galleries)) </code></pre> <p>You also want to be sorting after you run your items through a <code>set</code>, not before.</p> <pre><code>gallery_list = list(sorted(frozenset(gallery_list), key=attrgetter('display_order'))) </code></pre> <p>I would reconsider how you are going about fetching all of this data. You are doing two query for each grade level when you could probably replace it with one or two queries for all of the data. </p> <p>At the very least you could use a <code>for loop</code> to reduce the amount of code you have:</p> <pre><code>grade_titles = ['pre-k', 'k', '1', '2', '3', '4', '5', '6', '7', '8'] gallery_list = [] for grade_title in grade_titles: grade = GradeLevel.objects.prefetch_related().get(title=grade_title) grade_categories = grade_pk.grade_cats.filter(active=True,featured=True) grade_galleries = grade_pk.grade_curriculum.filter(active=True,featured=True) gallery_list.extend(grade_categories) gallery_list.extend(grade_galleries) gallery_list = list(sorted(frozenset(gallery_list), key=attrgetter('display_order'))) </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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