Basic principles of Django App Design


basic principle

Every app should do just one thing. Its function should be described clearly in a simple statement. If more than one “and” is used in the description process, it may mean that the app is a little big and needs to be split.

James Bennett:
The art of creating and maintaining a good Django app is that it should follow the truncated Unix philosophy according to Douglas McIlroy: ‘Write programs that do one thing and do it well.’

How to name Django app

  1. Try to use one word, such asanimals, blog, dreamspolls。 Simple and semantic project names are easier to maintain.
  2. If appropriate, you can refer to the name of the master data model in the app, which is plural.
  3. When naming, consider the form of URL. For example, if the URL of a blog can be, consider naming the app weblog instead of blog or posts.
  4. Use small letters for naming. Do not use numbers or other characters. Use underscores if necessary_But try to avoid using.

When hesitant, choose a small app solution

The separation and design of APP functions is an art, not a technology. Therefore, it may be necessary to split and reorganize in the future.

Try to make one app small enough, and multiple small apps are easier to maintain than one large app.

What are the modules in an app?

Common modules:

These modules can be seen in 99% of Django apps:

# Common modules

Less common modules

# uncommon modules

The function description of each module is as follows:

Modular Function description App level settings. If there are too many settings in app, they should be put in this file independently Decorator for app
Db / package Customized data model items and other database related components Place customized data model items. If there are not many database related components, you can only put them in this file without creating a DB package Factory function to generate test data Some auxiliary functions extracted from and Same as When the is large, you can extract the customized model managers Customized signal Mixins extracted from

Reference: two scoops of Django: Best Practices for Django 1.8