Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I get that models is a class with methods such as CharField(), which is being inherited into these classes in order to create table columns...right?</p> </blockquote> <p>I believe you took a wrong turn at Albuquerque. Lowercase <code>models</code> is not a class and <code>CharField()</code> is not a method, though it does end up calling a particular method called <code>__init__()</code> but we'll get to that in a bit.</p> <p>First, look at the import statement:</p> <pre><code>from django.db import models </code></pre> <p>Lowercase <code>models</code> here is a package, meaning it is a directory containing an <code>__init__.py</code> file. Specifically, it is a sub-package of the package <code>db</code>, which is itself a sub-package of the <code>django</code> package. The Python dotted path parallels the package directory structure.</p> <p>It is worth noting here that <code>models</code> is NOT a module, though it COULD have been. If there were a Python file named <code>models.py</code> within the <code>db</code> package, then <code>models</code> would have been a module. How do we know for sure? By perusing the Django source code, we find that it is a directory, and thus a package. <a href="https://github.com/django/django/tree/master/django/db/models" rel="nofollow">See the source code here</a>. </p> <p>So, packages are directories containing either: other directories (sub-packages) or python files (modules). Class definitions are defined in the modules. Ok, then how is the <code>Model</code> class imported from <code>models</code> if it is a package and not a module?</p> <p>This is possible because the <code>__init__.py</code> file in the <code>models</code> package imports the <code>Model</code> class. The <code>Model</code> class definition actually lives in the <code>base</code> module, yep the <code>base.py</code> file within the <code>models</code> package.</p> <p>Now, for the <code>CharField()</code>. In Python, class instantiation uses the function notation. When defining your custom <code>Model</code> classes, the database fields are created from your model's class attributes. Within the <code>Book</code> model, <code>title</code> is a class attribute, instantiated from the <code>CharField</code> class. When it is instantiated, the <code>CharField</code> method <code>__init__()</code> is called.</p> <p>And here is a good place to point out why it is handy to have packages for making imports more convenient. The <code>CharField</code> class is defined within the <code>fields</code> sub-package of the <code>models</code> package, and many of Django's other field classes are defined there as well. Similar to how <code>__init__.py</code> in the <code>models</code> package imported the <code>Model</code> class from the <code>base</code> sub-module, it also imports all the fields from the <code>fields</code> sub-module.</p> <p>Since the <code>models</code> package takes care of all the sub-imports we'll likely need to use, we can just import that package. Using the <code>from</code> style import, we can easily identify all the places in our code where we are using something from that package, since it always has the dot notation <code>models.Something</code>. We don't have to worry about how all that stuff is organized in the package.</p> <p>For more details, <a href="http://docs.python.org/2/tutorial/modules.html#packages" rel="nofollow">see the Python documentation on packages</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. 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