Reputation:
It might sound trivial, but I'd like to know where I am supposed put my python classes (not from Model
)?
I mean, my model classes are defined in myapp/models.py
but I want, for example, use a syndication feed framework to write a simple feed. The doc says that I need to write a Feed class but... Where should I put the code? Which path and file? I'd like to stick to the default Django directory structure.
Thanks!
Upvotes: 14
Views: 7314
Reputation: 680
Models.py
is the best place for your class, because models have access to data stored in the database.
Upvotes: 0
Reputation: 411242
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
Upvotes: 5
Reputation: 3266
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
Upvotes: 14