Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Some points to consider:</p> <p>The time it takes to import a module is, in almost all cases, completely irrelevant: it only happens once. Your Django view module isn't being imported and re-evaluated for every request; it's loaded once and then reused. If your modules are being reloaded constantly, something is catastrophically wrong.</p> <p>Every import is <em>not</em> taking 256kb of memory. Perhaps each individual file, loaded once, is (though I'd doubt that as well), but importing the same file repeatedly is not taking 256kb each and every time; it's merely creating a reference. If memory use is in question, simply profile it--load 10000 of something and see how much memory is used.</p> <p>For Django modules, you don't always need to create a directory for each; I import each model class from <code>models/__init__.py</code> (eg. <code>from Customer import Customer</code>), so I can say <code>from myapp.models import Profile, Customer, Book, ...</code>.</p> <hr> <p>The tendency for Django views to need a dozen lines of imports at the top really is a problem. It turns into <em>boilerplate</em>, code which you copy and paste every time you start a new file. Source code requiring boilerplate is a major flaw.</p> <p>At the same time, I strongly advise against what some people might recommend: <code>import django</code> and then using fully-qualified module names. The result is typing things like <code>django.core.urlresolvers.reverse</code>. When you find yourself regularly copying and pasting function names because they're so long, something has gone wrong.</p> <p>There's no single, clear, obviously-correct solution to this. Putting the things which are consistently used in another module is a valid solution, but there are legitimate problems with it: it's hard to see what's being imported, and where the results of the import are used.</p> <p>You'll probably find fewer gut-reaction objections if you import the "collection of modules" module itself--<code>import djangohelpers</code> or <code>import djangohelpers as dh</code>. Then, you're writing things like <code>dh.paginator</code>. It gives the names a clear scope, and makes it much easier to see where it's being used and where particular function names are coming from, which you lose with "import *".</p> <p>(You probably do want to import things like <code>Q</code> and <code>_</code> as bare names, though.)</p>
 

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