Archive for February, 2010

Unicode in Django, It’ll Getcha

Posted in Uncategorized on February 11th, 2010 by Nick – Be the first to comment

I’ve been having an insane time trying to squash a weird unicode bug in Beertraq. This was one of the first bugs that was found after starting the beta and it’s taken me this long to get it worked out. It all started with a cryptic-seeming error upon viewing the add-a-beer page:

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-9: ordinal not in range(128)

Real descriptive, right? Looking through the stack trace wasn’t much help either. It was a bunch of stuff pertaining to trying to render the template. One thing I did notice is that a brewer with a unicode name had recently gotten added and this error was only happening on the new beer form (which includes a ChoiceField of brewers), but the brewer was being displayed just fine on its own page.

After asking my friend and fellow Django-user Dan-o what he thought, he asked if I had __str__() and __unicode__() defined in my models. I had __str__() defined (generally a good idea) but not __unicode__(). I popped it in to my Brewer model as so:

class Brewer(models.Model):
  TYPE_CHOICES = (
    ('Macrobrewer', 'Macrobrewer'),
    ('Microbrewer', 'Microbrewer/Craft brewer'),
    ('Homebrewer', 'Homebrewer'),
    ('Unknown', 'Unknown'),
  )

  name = models.CharField("Brewer name", max_length=255)
  brewer_type = models.CharField("Type of brewer", max_length=25, choices=TYPE_CHOICES)
  city = models.CharField("Brewer's city", max_length=255, blank=True)
  approved = models.BooleanField(default=False)

  def __str__(self):
    return self.name

  def __unicode__(self):
    return self.name

  class Meta:
    ordering = ['name']

And guess what? My problems went away. I feel like an idiot for being foiled by something so simple that I overlooked. It makes sense too because __str__() is used quite a bit in forms where the model instance’s name needs to be magically generated. Without __unicode__() it would cause Django to barf on unicode names, just like it did on me.

Let this be a lesson to you. If you might EVER have ANY unicode text in your model instance’s names, for crap’s sake define __unicode__()!!

New whiteboard

Posted in Uncategorized on February 9th, 2010 by Nick – Be the first to comment

If you know me personally, you know that I can be pretty scatterbrained from time to time. I’ve desperately needed a whiteboard to keep my thoughts organized, and I’ve finally gotten one. I know, it’s not the most exciting thing in the world to talk about, but it should be a change in the right direction for me.

New Whiteboard

But, wait. What’s this? “Beertraq Road to Stable”?

Beertraq Road to Beta

(click for full-sized version)

Part of why I want to get my thoughts organized is because I want a good view of what all is left before I fully release Beertraq to the public. With this list looming over my head (literally) it will hopefully get my butt in gear to reach a stable release.