문제

I'm using Flask, SQLAlchemy and WTForms. I have a number of properties in my model object which are marked as unique and nullable=False. This works fine when creating a new row in the database but when I try to edit an existing object the validator on WTForms fails with

{'aproperty': [u'Already exists.']}

How can I make this validation pass without having to change my data model?

Update

Following the documentation was of no use to me.

도움이 되었습니까?

해결책

You need to associate the existing record with the form. Otherwise the validator has no way of knowing that you're updating an existing record instead of creating a new one. Something like the following should do the trick:

current_obj = ...
form = MyForm(request.form, obj=current_obj)
form.validate_on_submit():
    form.populate_obj(current_obj)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top