A Simple Project Management Tool With TurboGears – Setting Up the Models

I want you to love TurboGears, understand how to use TurboGears and how to become fully productive with it. For example, I can develop a fully fledged application that would take 3 weeks with something else in 1-1.5 weeks with TurboGears. For that, I think a project management tool fits best here. It requires working with a database, creating forms, authentication and authorization and so on.

Now, go ahead and create a project called “ideas”. What we are going to do is a simple, a very simple project management tool. We are going to create a Project model, a Task model.

cd tg2env
source bin/activate
paster quickstart -m ideas
# answer yes to authentication/authorization question.

We will “cd” into the “ideas/ideas” directory and create a forms module and create a project_form.py file while in there.


cd ideas/ideas
mkdir forms
touch forms/__init__.py
touch forms/project_form.py

Next, we are going to create a “project” module in ideas/model/ folder.

cd model && touch project.py
# project.py
# -*- coding: utf-8 -*-
"""Post model module."""

from ideas.model import DBSession, DeclarativeBase
from sqlalchemy import Column, ForeignKey
from sqlalchemy.types import Integer, Unicode, UnicodeText

class Project(DeclarativeBase):
    __tablename__ = 'projects'

    id = Column(Integer, primary_key=True)
    project_name = Column(Unicode(255))
    project_description = Column(UnicodeText)

    @classmethod
    def get_projects_for_dropdown(self):
        projects = DBSession.query(Project).all()
        project_list = [(project.id, project.project_name) for project in projects]
        return project_list

class Task(DeclarativeBase):
    __tablename__ = 'tasks'

    id = Column(Integer, primary_key=True)
    project_id = Column(ForeignKey("projects.id"))
    task_name = Column(Unicode(255))
    task_description = Column(UnicodeText)
    status = Column(Unicode(6))

What have we done?
We have created two models, 1) Project and 2) Task. We have imported the necessary modules from both our project and SQLAlchemy in order to be able to setup our models correctly. The very next step is to create the tables we have declared in the models. Go and open ideas/model/__init__.py file. Go to the end of file. (Here is a “vim” trick for you. When you press ESC and then type “:$”, you will go to the end of file.) Add the following line there:

from ideas.model.project import Project, Task

Run the following commands:


# in the top level ideas/ project directory
python setup.py develop
paster setup-app development.ini

“paster setup-app development.ini” will create all our tables, including the authentication tables. It will use the database backend defined in the development.ini file, line: 52. By default, it is SQLite.

In the next post, we will create the forms. Until then, take care and check out ToscaWidgets.

About these ads
This entry was posted in Python, SQLAlchemy, TurboGears and tagged , , , , , , . Bookmark the permalink.

2 Responses to A Simple Project Management Tool With TurboGears – Setting Up the Models

  1. Pingback: A Simple Project Management Tool With TurboGears – Setting Up the Forms | TurboGears Notes

  2. Pingback: Querying The Models | TurboGears Notes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s