views:

147

answers:

2

I'd like to move my models to a separate directory, similar to the way it's done with Rails to cut down on code clutter. Is there any way to do this easily?

Thanks, Collin

+4  A: 

I assume you're using the basic webkit and not Django or something fancy. In that case just create a subdirectory called models. Put any python files you use for your models in here. Create also one blank file in this folder called __init__.py.

Then in your main.py or "controller" or what have you, put:

import models

at the top.

You just created a python package.

Brandon Thomson
+1  A: 

Brandon's answer is what I do. Furthermore, I rather like Rails's custom of one model per file. I don't stick to it completely but that is my basic pattern, especially since Python tends to encourage more-but-simpler lines of code than Ruby.

So what I do is I make models a package too:

models/
models/__init__.py
models/user.py
models/item.py
models/blog_post.py

In the main .py files I put my basic class definition, plus perhaps some helper functions (Python's module system makes it much safer to keep quickie helper functions coupled to the class definition). And my __init__.py stitches them all together:

"""The application models"""
from user import User
from item import Item
from blog_post import BlogPost

It's slightly redundant but I have lots of control of the namespace.

jhs