def addContact(): form = AddContactForm() customer_choices = [(a.id, a.name) for a in Customer.query.filter_by(customer_type=CUSTOMER_TYPES['TYPE_CUSTOMER']).all()] customer_choices = [(0, '')] + customer_choices form.customer.choices = customer_choices if form.validate_on_submit(): contact = Contact() contact.first_name = form.first_name.data contact.surname = form.surname.data contact.phone = form.phone.data contact.email = form.email.data if form.customer.data and form.customer.data != '' and form.customer.data != 0: contact.customer_id = form.customer.data else: contact.customer_id = None db.session.add(contact) db.session.commit() flash(gettext("New contact successfully added.")) return redirect(url_for("contacts")) return render_template('settings/addContact.html', title=gettext("Add New Contact"), form=form)