0

How can i use a variable as the column name, when adding new entries to the table. I have a HTML form which posts the entered values. My code is as follows

db = SQLAlchemy(app)

class Budget(db.Model):
    id=db.Column("id", db.Integer, primary_key=True) 
    Casenumber=db.Column("Casenumber", db.Integer)
    Project=db.Column("Project", db.String(30)) 
    Price=db.Column("Price", db.Integer)     

attributes=["Casenumber","Project","Price"]
        
        for i in attributes:     
            new_entry=request.form.get(i)
            db_entry=Budget(i=new_entry)
            db.session.add(db_entry)
        db.session.commit()

I get this TypeError: 'i' is an invalid keyword argument for Budget. How do i get to read the value of i i.e "Casenumber" instead of just i.

1

1 Answer 1

0

I have figured it out

class Budget(db.Model):
    id=db.Column("id", db.Integer, primary_key=True) 
    Casenumber=db.Column("Casenumber", db.Integer)
    Project=db.Column("Project", db.String(30)) 
    Price=db.Column("Price", db.Integer)     

attributes=["Casenumber","Project","Price"]
        new_entry=Budget()
        for i in attributes:   
            setattr(new_entry, i,request.form.get(i))
        db.session.add(new_entry)   
        db.session.commit()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.