jazzblue
jazzblue

Reputation: 2437

django package organization

I plan to build my project in Django framework. However, I noticed that all Django packages have models.py file. Now, let say I have a set of general purpose functions that I share between several apps in the project and I plan to put these functions definitions in a separate package (or app for that matter?). So, should I create an app "general" and copy-paste these functions into the models.py file? Or can I just create a general.py file in the "general" app directory and leave models.py empty? What is the "Django" way to do that?

Thanks.

Upvotes: 0

Views: 328

Answers (3)

Dan Aronne
Dan Aronne

Reputation: 616

I plan to put these functions definitions in a separate package (or app for that matter?)

Before you decide to make this an app (and if you do decide to make it an app), I recommend you take a look at James Bennet keynote on Developing reusable apps and hist post on laying out an application. From one of his slides:

Should this be its own application?

  • Is it orthogonal to whatever else I’m doing?
  • Will I need similar functionality on other sites?
  • Yes? Then I should break it out into a separate application.

If you're cramming too much functionality in one single general purpose app, it might be better to split your general purpose app into multiple reusable apps.

Going back to your original question, Django is expecting a models.py file in every app. So you must have the file even if it's empty.

Inside your models.py, you should only have the application’s model classes. So, you wouldn't be following a best practice if you put inside models.py some miscellaneous code you want to reuse.

From the laying out an application post I mentioned before:

At the application level, I usually drop in a few more files depending on exactly what the application is going to be using:

  • If the application defines any custom manipulators, I put them in a file called forms.py instead of in the views file.
  • If there are multiple custom managers in the app, I put them in a file called managers.py instead of the models file.
  • If I’m defining any custom context processors, I put them in a file called context_processors.py.
  • If I’m setting up any custom dispatcher signals, they go in a file called signals.py.
  • If the application is setting up any syndication feeds, the feed classes go in a file called feeds.py. Similarly, sitemap classes go in sitemaps.py.
  • Middleware classes go in a file called middleware.py.
  • Any miscellaneous code which doesn’t clearly go anywhere else goes in a file or module called utils.

All of this does not answer directly your original question:

can I just create a general.py file in the "general" app directory and leave models.py empty?

But I hope this gives you additional information to make a decision that better fits your project and requirements.

Upvotes: 1

Bogdan Iulian Bursuc
Bogdan Iulian Bursuc

Reputation: 2275

I usually create a utils.py file under my main app that is created from the django-admin.py when starting the project.

Upvotes: 1

Amit Yadav
Amit Yadav

Reputation: 1861

models.py file is used to define the structure of database. So you should leave it for defining your database entries. You can make an app named generals and put general.py in that app, and from there you can use it by calling it in any app.

Upvotes: 1

Related Questions