Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The accepted answer is spot on about the regex, but since we're discussing optimization, I thought I should note that the code for checking whether a project exists could be modified to generate a much quicker query, especially in other contexts where you could be counting millions of rows needlessly. Call this 'best practices' advice, if you will.</p> <pre><code>p = Project.objects.filter(projectName__exact = aProjectName).count() if p &gt; 0: </code></pre> <p>could instead be</p> <pre><code>if Project.objects.filter(project_name__iexact=aProjectName).exists(): </code></pre> <p>for two reasons.</p> <p>First, you're not using <code>p</code> for anything so there's no need to store it as a variable as it increases readability and <code>p</code> is an obscure variable name and <a href="http://www.codinghorror.com/blog/2007/05/the-best-code-is-no-code-at-all.html" rel="nofollow noreferrer">the best code is no code at all</a>.</p> <p>Secondly, this way we only ask for a single row instead of saving the results to the queryset cache. Please see the official <a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#exists" rel="nofollow noreferrer">Queryset API docs</a>, <a href="https://stackoverflow.com/questions/14368205/django-any-difference-between-queryset-nonzero-and-queryset-exists/14369747#14369747">a related question on Stack Overflow</a> and <a href="https://groups.google.com/forum/#!msg/django-developers/L4irIYGPUWM/wK7fQ1q_SsYJ" rel="nofollow noreferrer">the discussion about the latter on the django-developers group</a>.</p> <p>Additionally, it is customary in python (and Django, naturally) to name your fields lower_cased_separated_by_underscores. Please see more about this on the Python Style Guide <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow noreferrer">(PEP 8)</a>.</p>
    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. 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