Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Nov. 1, 2005 19:44:10

Clint E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Catching IntegrityError?


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,                    forumsname = input.lower(),                    address1 = input,                    address2 = input,                    city = input,                    stateprovince = input,                    country = input,                    notes = input,                    postalcode = input,                    emailaddress = input,                    randomkey = GenPasswd2(32) )                if errors == 0:            try:                p.save()            except IntegrityError, e:   # this doesn't work!                errors = errors + 1                t = e            ....---end code---When I try the above code, I get the following error:NameError: global name 'IntegrityError' is not definedSo which is it? does it exist or not? I'm really confused here :)Clint

Offline

#2 Nov. 1, 2005 20:03:48

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Catching IntegrityError?


On 11/1/05, Clint Ecker <> 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-manipulatorsAdrian

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

Offline

#3 Nov. 2, 2005 03:28:37

Clint E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Catching IntegrityError?


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

On 11/1/05, Adrian Holovaty <> wrote:
>
> On 11/1/05, Clint Ecker <> 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
>

Offline

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support