Search code examples
pythonflasksqlalchemyflask-sqlalchemyflask-wtforms

How to read and write the data that the user has selected to the database? WTForms


Faced with such a situation, I can not understand what needs to be written so that I consider what I choose to use from the select.

Here are the working parts of the code:

Models.py

class Customer(db.Model):
    __tablename__ = 'customer'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    surname = db.Column(db.String(255), nullable=False)
    debt = db.Column(db.NUMERIC, nullable=False)
    last_account = db.Column(db.NUMERIC, nullable=False)
    last_bill_of_the_month = db.Column(db.DATETIME, nullable=False)
    rate = db.Column(db.NUMERIC, nullable=False)

    condition_id = db.Column(db.Integer, db.ForeignKey('condition.id_condition'), nullable=False)
    status_id = db.Column(db.Integer, db.ForeignKey('status.id_status'), nullable=False)
    payment_method_id = db.Column(db.Integer, db.ForeignKey('payment_method.id_payment_method'), nullable=False)
    payment_type_id = db.Column(db.Integer, db.ForeignKey('payment_type.id_payment_type'), nullable=False)

forms.py

class AddClient(FlaskForm):
    name = StringField(validators=[InputRequired()], render_kw={"placeholder": ""})
    surname = StringField(validators=[InputRequired()], render_kw={"placeholder": ""})
    debt = StringField(validators=[InputRequired()], render_kw={"placeholder": ""})
    last_account = StringField(validators=[InputRequired()], render_kw={"placeholder": ""})
    last_bill_of_the_month = StringField(validators=[InputRequired()], render_kw={"placeholder": ""})
    rate = StringField(validators=[InputRequired()], render_kw={"placeholder": ""})

    # condition_id = SelectField(validators=[InputRequired()], coerce=int)
    condition_id = SelectField('condition', choices=[])
    status_id = SelectField('status', choices=[])
    payment_method_id = SelectField('payment_method', choices=[])
    payment_type_id = SelectField('payment_type', choices=[])
    submit = SubmitField("add")

views.py

@app.route('/addclient', methods=['GET', 'POST'])
def addclient():
    formClient = AddClient()
    formClient.condition_id.choices = [(conditions.id_condition, conditions.list_condition) for conditions in
                                       Condition.query.all()]
    formClient.status_id.choices = [(statuses.id_status, statuses.list_status) for statuses in
                                    Status.query.all()]
    formClient.payment_method_id.choices = [(payment_methods.id_payment_method, payment_methods.list_payment_method) for
                                            payment_methods in
                                            Payment_method.query.all()]
    formClient.payment_type_id.choices = [(payment_types.id_payment_type, payment_types.list_payment_type) for
                                          payment_types in
                                          Payment_type.query.all()]

    if formClient.validate_on_submit():

        new_client = Customer(name=formClient.name.data, surname=formClient.surname.data, debt=formClient.debt.data,
                              last_account=formClient.last_account.data, last_bill_of_the_month=formClient.last_bill_of_the_month.data, rate=formClient.rate.data)
        db.session.add(new_client)
        db.session.commit()
        return redirect('/cust')

    return render_template('addclient.html', formclient=formClient)

addclient.html

<form method="POST" action="">
        {{formclient.hidden_tag()}}
        {{formclient.name()}}
        {{formclient.surname()}}
        {{formclient.debt()}}
        {{formclient.last_account()}}
        {{formclient.last_bill_of_the_month()}}
        {{formclient.rate()}}
        {{formclient.condition_id()}}
        {{formclient.status_id()}}
        {{formclient.payment_method_id()}}
        {{formclient.payment_type_id()}}
        {{formclient.submit()}}
    </form>

I can read and write text. And here is the data that I cannot select. How to do it correctly ?


Solution

  • this helped me:

    condition_id = SelectField('condition', choices=[], coerce=int)
        status_id = SelectField('status', choices=[], coerce=int)
        payment_method_id = SelectField('payment_method', choices=[], coerce=int)
        payment_type_id = SelectField('payment_type', choices=[], coerce=int)
    

    When selected, it will just return the number that refers to it