Custom error messages on Django’s auto generated modelforms

March 26th, 2008

As I’ve written before, I’ve been adventuring into the world of Python lately, namely with PyGTK, Zope/Plone, and Django. Although I’m a huge newbie with these technologies, I’ve been picking up stuff pretty fast and I love Django and the speed at which I’m developing an application with it.

So after I started doing some stuff with newforms, I hit a wall. Newforms lets you generate a from from a model, and represents most of the model’s attributes in the form attributes format, except for one very important field: error_messages (or, if it does, is not yet documented).

After looking around for a while, and using a couple of tips from #django @ freenode, I came up with a solution for my problem, and since I haven’t found any exact reference to this problem anywhere else, here it is:


class myForm(ModelForm):
..def __init__(self, *args, **kwargs):
....super(myForm, self).__init__(*args, **kwargs)
....self.fields['field_name'].error_messages = {'required': "This field is required"}
..class Meta:
....model = myModel

(Sorry for the lack of indentation. Wordpress code tag doesn’t seem to like spaces.)

Maybe it would be a good idea in the future to specify a model attribute to contain those messages.

Updates

January 26th, 2008

I know that apart from the usual “delicious link posts” this blog hasn’t been active, but I’m not giving up on it or anything. Like I wrote before, I blog when I have something worthy, and not just because I have to keep the blog active.

However, my main reason for not blogging has been the extreme lack of time I’ve had lately. That’s what you get for trying to do a thousand things at the same time. But I’m happy with how things are happening and coming together, so it’s been worth it.

Apart from work I’ve been doing, I’ve finally managed to actually start doing something with Python: a desktop app I’m developing with a friend (and which I’ll write about some other day) is almost done and Django is waiting for me a web project that it’s still on it’s planning phase.

Speaking of which, I’ve also had the chance to meet Ricardo from zone41 and I guess me and the other guys involved in that same project met him at the exact right time. Once again, more on that some other day. Everything’s still under wraps, but things are shaping up and I’m really confident in this.

In other news, my newly acquired laptop’s hard drive died. Yup, the thing was only 3 weeks old, and those nasty badsectors came along. It’s the third hard drive that has died on my possession in the last year, and the second 2.5″ Seagate (the other one was a 3.5″ Maxtor). Since the store didn’t had a replacement and the laptop is pretty much out of stock everywhere, I sent it to the warranty service trough the store where I bought it, but I’m now thinking I should’ve contacted Sony directly. Thanks to my good friend Miguel, I’m not “laptopless” and I’ve been using his 15″ HP which reminds me once again why I wanted to get a smaller laptop so badly.