182

Is there a preferred naming convention for creating a Django app consisting of more than one word? For instance, which of the following is preferred?

  1. my_django_app
  2. my-django-app Update: Not allowed syntactically
  3. mydjangoapp Recommended solution

While all of them may be options 1 and 3 are syntactically allowed, is there a preference? Looking at the way Django creates the table names by combining the app name and the model name with an underscore, I'm leaning against option #1.

Thoughts?

Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163

4 Answers4

177

They must be valid package names. That rules out 2 ("import my-django-app" would be a syntax error). PEP 8 says:

Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

So, 1 and 3 are both valid, but 3 would be the recommended approach.

thraxil
  • 4,971
  • 2
  • 19
  • 10
  • 1
    A helpful blog post about this issue, just to expand: http://streamhacker.com/2011/01/03/django-application-conventions/ – Akhorus Dec 23 '15 at 15:34
  • 7
    Hi @surfer190 . Two scoops of Django contains a ton of good practices. They choose the singular: blog. It is the same choice when you are creating a Model: Blog is preferred above Blogs. – Wim Feijen Jan 18 '17 at 17:14
  • 2
    In general, I would recommend using underscores, because they improve readability and they are commonly used, for example I use django_extensions and django_debug_toolbar a lot. – Wim Feijen Jan 18 '17 at 17:16
  • 18
    @WimFeijen, it seems that Two scoops of Django, at least [here](https://pt.slideshare.net/accavdar/django-best-practices-46944311), recommends using 'plural version of the app's main model' for app names, with exceptions like an app named blog, a 'good exception'. – Caco Mar 07 '17 at 19:23
  • I stand corrected. Thanks @Caco for looking into this and educating me. – Wim Feijen Mar 14 '17 at 10:26
  • Just curious any use the app name like "main" or similar name that can represents almost anything. coz sometime i feel like the app i am dealing with has larger scope than the named app. – Loonb Dec 16 '18 at 08:41
39

some good examples

  • graphene_django
  • users
  • orders
  • oauth2_provider
  • rest_framework
  • polls

in simple terms, app_name should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. also should have a short name and it can be a plural and singular name

Jamil Noyda
  • 3,321
  • 2
  • 21
  • 26
9

App directory names must be a valid Python package name. This means that option 2 is completely inadmissible as a package name, although it can still be used for other purposes, such as documentation. In the end it comes down to personal style. If you prefer option 3 then use it.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

My votes for 1 and 3, but you can check several popular apps: http://www.django-cms.org/ http://geodjango.org/

Guard
  • 6,816
  • 4
  • 38
  • 58
  • 2
    Even more apps are here: https://djangopackages.org/ . Mind that packages are often named using minuses: django-debug-toolbar, while you have to use "django_debug_toolbar" in your INSTALLED_APPS setting. – Wim Feijen Jan 18 '17 at 17:19