Cameron
Cameron

Reputation: 23

Understanding Numeric Datatype in SqlAlchemy

I have a class

class vw_invoice_header(db.Model):

    __tablename__ = "vw_invoice_header"
    tax_amount = db.column(db.Numeric)
    total_price = db.column(db.Numeric)
    invc_number = db.Column(db.String(40))
    term_code = db.Column(db.String(20))
    order_date = db.Column(db.Date)
    airway_bill = db.Column(db.String(40))
    inh_auto_key = db.Column(db.Integer,primary_key=True)

Now I get a result from an oracle db as below

invc = vw_invoice_header.query.filter_by(inh_auto_key=20643519).first()

I'm trying to use the value below to get a nicely formatted price in my jinja2 template

"{:8,.2f}".format(invc.total_price)

This throws an error, AttributeError: type object 'Numeric' has no attribute 'lower' I have no idea how to just print out this numeric :/ Im a newb to python been using it for a week.

Thanks Cameron

Upvotes: 1

Views: 2292

Answers (1)

Suever
Suever

Reputation: 65430

Your first two fields should be db.Column not db.column (note the capitalization). db.column creates a sqlalchemy.sql.elements.ColumnClause object whereas db.Column creates a sqlalchemy.sql.schema.Column object like you want.

class vw_invoice_header(db.Model):

    __tablename__ = "vw_invoice_header"
    tax_amount = db.Column(db.Numeric)
    total_price = db.Column(db.Numeric)
    invc_number = db.Column(db.String(40))
    term_code = db.Column(db.String(20))
    order_date = db.Column(db.Date)
    airway_bill = db.Column(db.String(40))
    inh_auto_key = db.Column(db.Integer,primary_key=True)

Once you correct this, the Numeric datatype will behave just as you expect.

Upvotes: 2

Related Questions