Friday, April 29, 2011

Place for Django classes

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!

From stackoverflow
  • 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 : I suggest not, it makes lot of confusion. Is cleaner to have this: project.myapp.feeds than project.myapp.classed.feeds Eventually you can do this: project.myapp.extras.feeds project myapp extras feed
    Andrea Di Persio : I updated my answer.
    Jarret Hardie : +1 for project.myapp.feeds
  • 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
    

0 comments:

Post a Comment