Xtreme schrieb:
> Hi:
>
> Im trying to find a good soul, that explain to me how I can intercept
> a error raised by the sqlalchemy engine when the integrity of the
> table fail (i.e A user try to add the same value for the unique
> field).
>
> I don't want manually search if exist the record before add another
> only for not raise the error.
>
> I have a table with 2 fields:
>
> estados_civiles_table = Table ('estados_civiles', metadata,
> Column('id', Integer, primary_key=True),
> Column('estado_civil', Unicode(20), unique=True)
> )
>
> this table is mapped to EstadoCivil
> Plz note that estado_civil is a unique field
>
> then I have this class and controller for my forms and validators
>
> class NuevoEstadoCivil_Fields(widgets.WidgetsList):
> id = widgets.HiddenField()
> estado_civil =
> widgets.TextField(validator=validators.UnicodeString(not_empty=True,
> max=10, strip=True), attrs=dict(maxlength=20))
>
> NuevoEstadoCivil_form = widgets.TableForm(
> fields = NuevoEstadoCivil_Fields(),
> action = "save_NuevoEstadoCivil"
> )
>
> -----------------------------------
> @expose(template="bion.templates.nuevoestadocivil")
> def nuevoestadocivil(self, tg_errors=None):
>
> if tg_errors:
>
> flash(tg_errors)
>
> return dict(
> mainmenu=main_menu(menues=Menu.select(), parent=123),
> form=NuevoEstadoCivil_form
> )
>
>
> @expose()
> @validate(form=NuevoEstadoCivil_form)
> @error_handler(nuevoestadocivil)
> def save_NuevoEstadoCivil(self, **datos):
> try:
> estado_civil=datos['estado_civil']
> EstadoCivil(estado_civil=estado_civil)
> except Exception:
> flash ('El valor introducido no se agrego, porque ya
> existia')
>
> redirect(url('/estadosciviles'))
>
> Why the try not work?
> I use the Exception exception, because i think that is the more
> generic.
> Exist another short way to do it?
Just a wild guess - but are you sure that the SA-Exception thrown (or
the underlying database-adapters) is subclass of Exception? And it seems
as if you are after cathing *ALL* exceptions. Which is generally
considered bad style. There are use-cases, and yours might be one - but
you should at least try to catch the specific exceptions.
For cathing all exceptions, you do
try:
...
except:
...
note the missing Exception.
Diez
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---