Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your question has more to do with how Python works than how to use Django. So before I explain how to apply imports in Django here is how you do them in Python.</p> <p>Python has extremely flexible modularization system - or a capability to break your code base into many files with hierarchy. This is rather necessary in any programming language because having your code in multiple files helps to organize things and it definitely helps when you have a group of people working on a project but the way Python does it is extremely easy and powerful (if you need it to be).</p> <p>The most basic component in Python import system are modules. Any Python file (with <code>.py</code> extension) is a Python module. The module name is just a name of the file excluding the extension. For example <code>hello.py</code> file is a <code>hello</code> Python module. Now modules/files usually have some variables/functions/classes defined inside of them. You can easily import those in other Python modules using an <code>import</code> statement. Image you have two files in the same folder - <code>foo.py</code> and <code>bar.py</code>. The following is a content of <code>foo.py</code>:</p> <pre><code># foo.py hello = 'world' def sum(a, b): return a + b </code></pre> <p>Then if you need to use anything what is defined in <code>foo.py</code> in your <code>bar.py</code> file you can do so:</p> <pre><code># bar.py # the following import the whole foo module (foo.py file) import foo number = foo.sum(1, 2) </code></pre> <p>As you can see inside the <code>bar.py</code>, you can import the whole <code>foo</code> module and use anything what is defined in it. This however is usually not the best method to import things. Imagine in your <code>foo</code> module you will have 10,000 variables defined however you only need to use one of them. So if you will import the whole module, Python will have to import all the variables which will obviously eat more memory then necessary. To solve that, you can selectively import objects from other objects using the <code>from .. import ..</code> statement. The following example in <code>bar.py</code> only import the <code>sum</code> function from module <code>foo</code>:</p> <pre><code># bar.py # import only the sum function from foo module from foo import sum number = foo.sum(1, 2) </code></pre> <p>So importing from Python modules is pretty easy. Now to put that in your Django context, within your app you should have two files - <code>models.py</code> and <code>forms.py</code>. Inside <code>models.py</code> you should define all your models:</p> <pre><code># models.py from django.db import models class FooModel(models.Model): pass </code></pre> <p>Within the <code>models.py</code> the way <code>models</code> is imported is different than what I showed before. In this case Python actually imports from a Python package however I will not get into that not to complicate things. Just take that import statement for granted.</p> <p>Now that you have models defined you can create forms for them in <code>forms.py</code>:</p> <pre><code># forms.py # import django forms - take this statement for granted from django import forms # import the model from models import FooModel class FooModelForm(forms.ModelForm): class Meta: model = FooModel </code></pre> <p>That is it. For more information about how Python does imports you can read the Dive Into Python book <a href="http://www.diveintopython.net/" rel="nofollow">here</a> (it also has a lot of other useful information...).</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