Conventions
- Read and pay attention to current code in the repository
- For the Python part, we follow pep8 in most cases. We use flake8 to check for linting errors. Once you’re ready to commit changes, check your code with
flake8. - Install a plugin for EditorConfig and let it handle some of the formating issues for you.
- For the Django part, we follow standard Django coding style.
- And always remember the Zen.
Coding Rules
Django models
- All model names in singular an CamelCase.
- All models have a
Metawith at least:verbose_nameandverbose_name_plural: unicode strings, lowercase, with spaces.ordering: return a consistent order, using pk if no other unique field or combination exists.
- All models have
__unicode__method, returning a human-readable, descriptive, short text. - All fields have
verbose_name. Alsohelp_textif needed to fully explain the field meaning. -
All fields have explicit
blankandnullparameters. Use only those combinations, unless there a documented need of other thing:Normal fields (IntegerField, DateField, ForeignKey, FileField…)
- (optional)null = True,blank = True
- (required)null = False,blank = FalseText fields (CharField, TextField, URLField…)
- (optional)null = False,blank = True
- (required)null = False,blank = FalseBoolean fields:
- (two values, T/F)null = False,blank = True
- (three values, T/F/Null)null = True,blank = True -
Don’t create text fields with
null = True, unless you need to distinguish between empty string andNone. - Don’t create boolean fields with
blank = False, otherwise they could only beTrue.
Example:
class SomeClass(models.Model):
name = models.CharField(max_length=100, null = False, blank = False, unique=True,
verbose_name = _(u'name'))
slug = models.SlugField(max_length=100, null = False, blank = False, unique=True,
verbose_name = _(u'slug'),
help_text = (u'Identifier of this object. Only letters, digits and underscore "_" allowed.'))
text = models.TextField(null = False, blank = True,
verbose_name = _(u'text'))
class Meta:
verbose_name = _(u'some class')
verbose_name_plural = _(u'some classes')
ordering = ['name']
def __unicode__(self):
return self.name