Skip to content

Instantly share code, notes, and snippets.

@mattbriancon
Last active August 29, 2015 14:15
Show Gist options
  • Save mattbriancon/37075f145223476d3693 to your computer and use it in GitHub Desktop.
Save mattbriancon/37075f145223476d3693 to your computer and use it in GitHub Desktop.
Pythonic Django

Pythonic Django

Purpose

Links

PEP8 Ambiguities, Unambiguated

Single-quotes first

PEP 8 does not make a recommendation as to which type of quotation mark to use ('single-quotes' or "double-quotes"), only that one should be chosen and it should be used consistently throughout the codebase.

Use 'single-quotes' first and only use "double-quotes" when it would dramatically complicate the code to properly escape a single-quoted string (applies to docstrings too).

class SomeClass(object):
	'''
	You do add documentation, right?
	'''
	
	def bar(self):
		'''
		
		'''
Example:
reasonable = 'Isn\'t Python the greatest?'
unreasonable = '\'Please\' \'don\'t\' \'do\' \'this\''
made_reasonable = "'Please' 'don't' 'do' 'this'"

Docstrings for all non-trivial classes, methods, and functions

# good!
def foo(bar, baz):
	'''
	
	'''

	return

# bad!
def foo(bar, baz):
	'''asdf'''

Descriptive variable names

Avoid names like to_ret especially when the variable is long-lived.

Practical Django

The following suggestions reflect the conventions set by the documentation and source code of Django and other third-party packages and

Imports

The recommendations in PEP 8 should be taken first. The directory structure encouraged/imposed by Django makes it easy to follow the same ordering of imports across all files. Ignore the imports and froms when alphabetizing a group of imports.

  1. __future__ imports: All files must import absolute_import and unicode_literals in preparation for an eventual move to Python 3.
  2. Python Standard Library
  3. Django
  4. Third-party packages
  5. Other apps
  6. Relative imports
Example:
from __future__ import absolute_import, unicode_literals

# python stdlib
from datetime import timedelta
import logging

# django
from django.db import models
from django.utils import timezone

# other third party
from tasty.resources import ModelResource

# apps within the project
from core.models import Company
from schedules.models import Event

# imports relative to the current file
from .models import Invoice
from .utils import partition

TestCase subclasses should be plural

Example:
# good!
class AwesomeTests(TestCase):
	# ....

# bad!
class BadTest(TestCase):
	# ...

TastyPie Resources live in app/resources.py

All Resource subclasses should exist in a file called resources.py under the appropriate app directory. In this case, we're simply following the convention set forth by TastyPie to make our code line up more closely with the documentation.

utils

Apps can each have a utils.py to collect useful functionality used across the modules of the app. Note: we follow the Django convention of using utils.py not util.py.

Django-specific

Model Fields

  • CharField should never use null=True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment