tags:

views:

162

answers:

2

Hi!

I have a little noob (and perhaps trivial) question. Where I should put my Django classes (not from Model)?

I mean, my model classes are defined in myapp/models.py but I want, for example, use the syndication feed framework to write a simple feed. The doc says that I need to write a Feed class but... where I should put the code? which path and file? I want to kept the default Django directory structure.

Thanks!

+3  A: 

You are free to put it where you want.

example:

project
  myapp 
     views.py
     models.py
     feeds.py

Then you can import you're feeds module by using import.

from project.myapp.feeds import *

Making directory is better when you deal with a lot of file. For example:

project
    myapp
      models.py
      views.py
      extras
        feeds.py
        sitemaps.py

I suggest you to read this: Python Modules: Packages

Andrea Di Persio
So... there is no any Django convention for it? If I have several classes, should I make a "classes" folder under myapp dir? Thanks!
I suggest not, it makes lot of confusion.Is cleaner to have this:project.myapp.feedsthanproject.myapp.classed.feedsEventually you can do this:project.myapp.extras.feedsproject myapp extras feed
Andrea Di Persio
I updated my answer.
Andrea Di Persio
+1 for project.myapp.feeds
Jarret Hardie
Ok, you convince me, a lot of thanks! :D
A: 

For feed-related classes, it's conventional to place them in a module called feeds in the appropriate application module. For example, if you have a blog with an RSS feed, your project structure would look like this:

project/
    blog/
        __init__.py
        feeds.py
        models.py
        views.py

In general, for other non-model classes, you can put them anywhere you want (as noted by Andrea Di Persio). Typically you place them as a module in the appropriate application package. For general-use classes, I usually create a package called lib that contains general classes and functions, like so:

project/
    blog/
        __init__.py
        feeds.py
        models.py
        other_stuff.py
        views.py
    lib/
        __init__.py
        things.py
mipadi
Thanks for the advices!