Catching IntegrityError?

1,336 views
Skip to first unread message

Clint Ecker

unread,
Nov 1, 2005, 2:43:56 PM11/1/05
to django...@googlegroups.com
I've got a model that looks like the following and describes a "Person", notice that I require the forumsname to be "unique"

---begin code---

class Person(meta.Model):
    fullname        = meta.CharField(maxlength=100,verbose_name="Full Name",blank=False)
    forumsname      = meta.CharField(maxlength=100, blank=False, unique=True)
    address1        = meta.CharField(maxlength=255, blank=False)
    address2        = meta.CharField(maxlength=255)
    city            = meta.CharField(maxlength=255, blank=False)
    stateprovince   = meta.CharField(maxlength=100, blank=False)
    postalcode      = meta.CharField (maxlength=32, blank=False)
    country         = meta.CharField(maxlength=100, blank=False)        
    emailaddress    = meta.EmailField(blank=False)
    notes           = meta.TextField()
    beenemailed     = meta.BooleanField(default=False)
    randomkey       = meta.CharField(maxlength=32)
   
...

--end code--


In my view, I do something similar to the following (instantate a new Person, fill in the information from a form, and try to save it).  When someone enters a forumsname that's already in the database, I'd like to catch that, and act appropriately.  So initially, I didn't have a try/catch block so I could see what the exception was.  It told me that it would be throwing an "IntegrityError" so I modified my code to look like this:

---begin code---
p = persons.Person( fullname = input['fullname'],
                    forumsname = input['forumsname'].lower(),
                    address1 = input['address1'],
                    address2 = input['address2'],
                    city = input['city'],
                    stateprovince = input['stateprovince'],
                    country = input['country'],
                    notes = input['notes'],
                    postalcode = input['postalcode'],
                    emailaddress = input['emailaddress'],
                    randomkey = GenPasswd2(32) )
        
        if errors == 0:
            try:
                p.save()
            except IntegrityError, e:   # this doesn't work!
                errors = errors + 1
                t['forumsnameerror'] = e
            ....

---end code---

When I try the above code, I get the following error:
NameError: global name 'IntegrityError' is not defined


So which is it? does it exist or not? I'm really confused here :)

Clint

Adrian Holovaty

unread,
Nov 1, 2005, 3:03:37 PM11/1/05
to django...@googlegroups.com
On 11/1/05, Clint Ecker <clint...@gmail.com> wrote:
> In my view, I do something similar to the following (instantate a new
> Person, fill in the information from a form, and try to save it). When
> someone enters a forumsname that's already in the database, I'd like to
> catch that, and act appropriately. So initially, I didn't have a try/catch
> block so I could see what the exception was. It told me that it would be
> throwing an "IntegrityError" so I modified my code to look like this:
> [...]
> When I try the above code, I get the following error:
> NameError: global name 'IntegrityError' is not defined

Two things here:

First, you're getting "global name 'IntegrityError' is not defined"
because IntegrityError isn't in the local namespace when you're
catching it. You'll need to import it -- probably from psycopg:

from psycopg import IntegrityError

Second, it's bad practice to wrap save() in try/except. The save()
methods don't perform validation -- they expect data to be valid
already. Use a manipulator to validate your data. (A manipulator
encapulates validation and form display.) See the docs here:
http://www.djangoproject.com/documentation/forms/#custom-forms-and-manipulators

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Clint Ecker

unread,
Nov 1, 2005, 10:12:02 PM11/1/05
to django...@googlegroups.com
Thanks a bunch! I rewrote my app using the manipulators and such and
it cut out a ton of stuff I'd written!

I guess I should read through the docs a bit more often ;)

Clint
Reply all
Reply to author
Forward
0 new messages