Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <a href="https://docs.djangoproject.com/en/1.11/ref/models/querysets/#count" rel="noreferrer"><code>count()</code></a>:</p> <pre><code>sc=scorm.objects.filter(Header__id=qp.id) if sc.count() &gt; 0: ... </code></pre> <p>The advantage over e.g. <code>len()</code> is, that the QuerySet is not yet evaluated:</p> <blockquote> <p><code>count()</code> performs a <code>SELECT COUNT(*)</code> behind the scenes, so you should always use <code>count()</code> rather than loading all of the record into Python objects and calling <code>len()</code> on the result.</p> </blockquote> <p>Having this in mind, <a href="https://docs.djangoproject.com/en/1.11/ref/models/querysets/#s-when-querysets-are-evaluated" rel="noreferrer"><strong>When QuerySets are evaluated</strong></a> can be worth reading.</p> <hr> <p>If you use <code>get()</code>, e.g. <code>scorm.objects.get(pk=someid)</code>, and the object does not exists, an <code>ObjectDoesNotExist</code> exception is raised:</p> <pre><code>from django.core.exceptions import ObjectDoesNotExist try: sc = scorm.objects.get(pk=someid) except ObjectDoesNotExist: print ... </code></pre> <p><strong>Update:</strong> it's also possible to use <a href="https://docs.djangoproject.com/en/1.11/ref/models/querysets/#exists" rel="noreferrer"><code>exists()</code></a>:</p> <pre><code>if scorm.objects.filter(Header__id=qp.id).exists(): .... </code></pre> <blockquote> <p>Returns <code>True</code> if the QuerySet contains any results, and <code>False</code> if not. This tries to perform the query <strong>in the simplest and fastest way</strong> possible, but it does execute nearly the same query as a normal QuerySet query.</p> </blockquote>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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