Brad's Blog

web dev, django, running, food, or whatever

Pretty options for Django's auth.User 2010-04-22

Several of my Django Apps have Foreign Key relationships to django.contrib.auth.model.User. In Django's admin app, these show up a select elements displaying the username attribute. For some people, that may be OK, but for most of the people with which I work, it's not. We want to see prettier options, i.e. each User's full name as the options in that select element.So, here's how it works. We override the ModelChoiceField (for ForeignKeys) and the ModelMultipleChoiceField (for ManyToMany Fields):from django.forms import ...

Dealing with Unicode and ASCII using Python 2010-03-25

Dealing with Character Encodings is (sometimes) hard. It's especially confusing for those who've never done it before. Converting text from unicode to ascii can be tricky. A lot of times, I'll import some data from a text file, and I just want to convert everything to ASCII and ignore anything that's not ascii (like MS Word's smart quotes). Luckily, this is fairly easy:mystring = mystring.decode('ascii', 'ignore')There's tons of great Python resources (and code!) for all your character encoding needs. In ...

On select_related() 2010-03-23

If you use Django, and your models have relationships that span across multiple tables, you need to read this: http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4select_related() is awesome.That is all.

Building PIL on OS X: Snow Leopard 2010-02-25

There are several places online that discuss problems installing PIL on Mac OS X Snow LeopardThis is how I got it to work.Install lib jpeg using homebrew (which is super-aweseome!) brew intall jpeg.This installs the library into /usr/local/Cellar/jpeg/7Install libfreetype the old-fashioned way (./configure, make, sudo make install). I used freetype-2.1.10.pre-20050511.Download, unpack PIL (I used Imaging-1.1.6). I had to make the following changes to setup.pyFREETYPE_ROOT = "/usr/local"JPEG_ROOT = ("/usr/local/Cellar/jpeg/7/lib", "/usr/local/Cellar/jpeg/7/include")Then, build PIL:python setup.py build_ext -iIf the build works without any errors, ...

Specialization 2010-02-24

Thanks to @mxcl for tweeting a link to this: http://gist.github.com/313640Things I've done (or similar)change a diaperplan an invasion (warcraft counts, right?)butcher a hog squirreldesign a building (with legos!)write a sonnet songbalance accountsbuild a wall fencecomfort the dyingtake ordersgive orderscooperateact alonesolve equationsanalyze a new problempitch manureprogram a computercook a tasty meal (yum!)fight efficiently (yay for taekwondo!)...not quite there yet:conn a shipset a bonedie gallantly (here's hoping!)Hooray for Humanity!

Adventures in Bread-Baking: Part 1 2010-01-23

Last weekend I attempted to make Mark Bittman's Speedy No-Knead Bread... sort of. Being the adventurous experimenter that I am, I altered the ingredients:2 cups whole-grain flour + 1 cup all purpose flour2 cups of really warm water (instead of the 1.5 cups)I followed the rest of the recipe perfectly. However, the results were really unsatisfactory. The center was super dense and gooey, and the bread had a really really hard crust (ok, I also cooked it an extra 30 ...

Add JSON syntax highlighting in Vim on OS X 2010-01-15

This is how I got Vim to do syntax highlighting for JSON files (Max OS X - Snow Leopard)Download the syntax file from http://www.vim.org/scripts/script.php?script_id=1945. (I got json.vim version 0.4)If it doesn't already exist, create a .vim/ftplugin in your home directory.Put json.vim into .vim/ftpluginAdd the following lines in your .vimrc file (mine is located at ~/.vimrc)au BufRead,BufNewFile *.json set filetype=jsonau! Syntax json source /Users/brad/.vim/ftplugin/json.vimNote that you'll have to change the path to your json.vim file so that it works on your ...

Resize the iTunes Window 2010-01-14

My Macbook Pro is my main machine. At the office, I connect it to a 20" Cinema display. At home, I connect it to a 24" Samsung Monitory (Model 2494SW Glossy Black, which I really like, btw.)There's a down-side to all this, though. I mostly just notice it with iTunes. When I'm working on the laptop without an external monitor, my iTunes window is larger than my screen :( Unfortunately, clicking the Maximize button doesn't do what I want (which ...

Ubuntu Audio, YES! Windows XP, NO. 2009-12-13

I run a dual-boot Ubuntu64[1] and Windows XP system. The windows partition really exists for a single purpose, and I occasionally just reboot the machine, choose the XP partition from the Grub menu, and all is well.However, a few weeks ago, I upgraded Ubuntu, but when I rebooted the machine and chose the XP partition, I noticed the sound[2] stopped working. What!? The audio worked fine in ubuntu!The Secret: I had to completely power down the machine before booting into ...

Gahhh!! Django, virtualenv, and cx_Oracle 2009-11-24

UPDATE: Thanks in advance to the comments from Graham Dumpleton whose comments below pointed me in the right direction!This wasis a plea for help.I've got django installed and configured with apache and virtualenv. I also have one particular app (named myapp) that queries an Oracle database directly (django is configured to use MySQL). All of the apps work, except for anything thatbrequires the myapp app... which includes the admin!Requesting any view that uses cx_Oracle results in a cryptic error simiar ...