[Answered ]-Split Django models and modelforms in separate file

2👍

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.

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).

The most basic component in Python import system are modules. Any Python file (with .py extension) is a Python module. The module name is just a name of the file excluding the extension. For example hello.py file is a hello 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 import statement. Image you have two files in the same folder – foo.py and bar.py. The following is a content of foo.py:

# foo.py

hello = 'world'

def sum(a, b):
    return a + b

Then if you need to use anything what is defined in foo.py in your bar.py file you can do so:

# bar.py

# the following import the whole foo module (foo.py file)
import foo

number = foo.sum(1, 2)

As you can see inside the bar.py, you can import the whole foo module and use anything what is defined in it. This however is usually not the best method to import things. Imagine in your foo 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 from .. import .. statement. The following example in bar.py only import the sum function from module foo:

# bar.py

# import only the sum function from foo module
from foo import sum

number = foo.sum(1, 2)

So importing from Python modules is pretty easy. Now to put that in your Django context, within your app you should have two files – models.py and forms.py. Inside models.py you should define all your models:

# models.py

from django.db import models

class FooModel(models.Model):
    pass

Within the models.py the way models 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.

Now that you have models defined you can create forms for them in forms.py:

# 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

That is it. For more information about how Python does imports you can read the Dive Into Python book here (it also has a lot of other useful information…).

Leave a comment