03 oct 07
Recently at work I talked a bit about some Django best practices, both in general and for us internally. It’s cool to work with other people with Django, to see what they pick up on and what they like/don’t like. Here are a couple of tips, which you may have heard before, but probably bear repeating.
1. Don’t use null=True on string-based fields, unless you have a good reason (which you don’t). This is mentioned in the docs, but it is oft-missed. This creates a situation where there are two possible empty options: NULL or empty string. The Django convention is empty string, so don’t use NULL.
2. Read the difference between ‘blank’ and ‘null’. Or, in a nutshell, think of null as a database-related option, and blank as an admin (validation)-related option.
3. You can set a verbose field name on a ForeignKey or ManyToManyField by using “verbose_name”. On other field types, the optional first positional argument is assumed to be this. Here’s one I’m annoyed I didn’t figure out myself/find in the docs myself.
4. One of the things I am nuts about is slugging your models up. Django makes it so easy to have readable (and seo-friendly) urls, so why not do it?
5. For the longest time, I didn’t realize this one: you can display multiple fields on the same line in the admin. The Django admin is beautifully designed and user-friendly as is, but you can make it even easier for people by utilizing all of the admin options. Grouping can make a model with a lot of fields easier to step through.
Shaun / 03 oct 2007 / 10:27 p.m.
Isn’t slugging your models what Calvin Klein does when he gets mad?