I've just posted this same question on the sqlalchemy group... I don't
know where it belongs:
> I'm using SQLAlchemy with Pylons and am having trouble validating
> data. I have an App object mapped to a table with a unique constraint
> on App.number.
>
> Here's some code:
>
> q = Session.query(App)
> if app_id:
> q = q.filter_by(id=app_id).first()
> if q:
> c.app = q
> number = request.params.get('number')
> notes = request.params.get('notes')
> if appmodel and number:
> try:
> q.number = number
> q.notes = notes
> Session.save(q)
> Session.commit()
> c.message = 'Record updated'
> except:
> # restore pre-form data ?? how??
> c.message = 'Error updating record'
> return render('index.mtl')
> else:
> return self.index()
>
> My questions are:
>
> 1) When I do the try statement, the value of q.number changes to
> whatever the user passed in via the form -- even if it's invalid, so
> that when I render the page, the invalid value is used. How do I
> reset the object to have the values it had before I did the try? Do I
> have to get it afresh from the db?
>
> 2) How do I let the user know which value caused the record not to
> update? What information does SQLAlchemy provide back that I can use
> to say: "You're number must be unique... and such-and-such must be
> greater than 0, etc..?"
>
> Thanks,
>
> Matt Haggard
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" 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/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---