سؤال

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