Pegase745 / sqlalchemy-datatables

SQLAlchemy integration of jQuery DataTables >= 1.10.x (Pyramid and Flask examples)
MIT License
159 stars 67 forks source link

"error":"'list' object has no attribute 'add_columns'" #114

Closed Alkasih closed 5 years ago

Alkasih commented 5 years ago

I am using Flask

I always get error message "error":"'list' object has no attribute 'add_columns'" I tried to googling it, but seems no one have similar issue with me.

I am using mysql.

    from __future__ import print_function # In python 2.7
    from flask import Blueprint, jsonify, Flask, request
    from app import db, session
    from datatables import ColumnDT, DataTables

    from app.mod_rawlogs.rawlogmodels import Rawlog

    mod_rawlogs = Blueprint('rawlogs', __name__, url_prefix='/rawlogs')
    @mod_rawlogs.route('/', methods=['GET', 'POST'])

    def index():

        columns = [
            ColumnDT(Rawlog.id),
            ColumnDT(Rawlog.one),
            ColumnDT(Rawlog.two),
            ColumnDT(Rawlog.three)
        ]

        query = session.query(Rawlog).all()
        # GET parameters
        params = request.args.to_dict()
        #return jsonify(params); 
        # instantiating a DataTable for the query and table needed
        rowTable = DataTables(params, query, columns)
        # returns what is needed by DataTable
        response = jsonify(rowTable.output_result())
        response.headers.add('Access-Control-Allow-Origin', '*')
        return response

`

And I always end up accessing it by passing draw parameter:

  http://mydomain.com/rawlogs/?draw=1

And it give result

      `"error":"'list' object has no attribute 'add_columns'"`

Here is my model

    from app import db
    from sqlalchemy import Column, DateTime, Integer, String
    from sqlalchemy.schema import FetchedValue
    from flask_sqlalchemy import SQLAlchemy

    #db = SQLAlchemy()

    class Rawlog(db.Model):
        __tablename__ = 'anything'

        id = db.Column(db.Integer, primary_key=True)
        datetime = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
        _from = db.Column('from', db.String(20), nullable=False)

        def __unicode__(self):
            """Give a readable representation of an instance."""
            return '%s' % self.imsi

        def __repr__(self):
            """Give a unambiguous representation of an instance."""
            return '<%s#%s>' % (self.__class__.__name__, self.id)

`

Alkasih commented 5 years ago

https://stackoverflow.com/questions/53939874/sqlalchemy-datatables-list-object-has-no-attribute-add-columns

Pegase745 commented 5 years ago

do not execute .all() in your query = session.query(Rawlog).all(). It returns a list, which explains the error. You must construct a query, and the lib executes in the end the all()

Alkasih commented 5 years ago

@Pegase745 Thank you for the respond. I actually had changed the way I query it by session.query().select_from(Rawlog) And now it return another error {"draw":"2","error":"int() argument must be a string or a number, not 'NoneType'","recordsFiltered":"322687","recordsTotal":"322687"}

Alkasih commented 5 years ago

I have tried this,

Rawlog.query.with_entities(Rawlog.id, Rawlog.one, Rawlog.two, Rawlog.three)

but the error is the same

{"draw":"2","error":"int() argument must be a string or a number, not 'NoneType'","recordsFiltered":"322687","recordsTotal":"322687"}

Pegase745 commented 5 years ago

Could you paste the correct models.py file please ? It's not clear between the one here, and the one on stackoverflow. It seems like an issue in your model definition, and not the way you query

Alkasih commented 5 years ago

@Pegase745 Thank you, again. Here is my model.

# coding: utf-8
from app import db
from sqlalchemy import Column, DateTime, Integer, String
from sqlalchemy.schema import FetchedValue
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import inspect, ForeignKey

#db = SQLAlchemy()

Base = declarative_base()

class Base(db.Model):
    __abstract__  = True
    id            = db.Column(db.Integer, primary_key=True)

class Rawlog(Base):
    __tablename__ = 'rawlogs'

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(640, u'utf8_unicode_ci'), nullable=False)
    sent = db.Column(db.DateTime, nullable=False)
    received = db.Column(db.DateTime, nullable=False)
    readable_content = db.Column(db.String(640), nullable=False)
    hantu = db.Column(db.Integer, server_default=db.FetchedValue())

    def __init__(id, content, sent, received, readable_content, hantu):

        self.id = id
        self.content     = content
        self.received    = received 
        self.readable_content = readable_content  
        self.sent = sent
        self.hantu = hantu

And here is the app db

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://sudo:password@hostandport/db'
app.config["SQLALCHEMY_ECHO"] = False

db = SQLAlchemy(app)
Pegase745 commented 5 years ago

Have you tried something as simple as that ?

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Rawlog(db.Model):
    __tablename__ = 'rawlogs'

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(640, u'utf8_unicode_ci'), nullable=False)
    sent = db.Column(db.DateTime, nullable=False)
    received = db.Column(db.DateTime, nullable=False)
    readable_content = db.Column(db.String(640), nullable=False)
    hantu = db.Column(db.Integer, server_default=db.FetchedValue())
Alkasih commented 5 years ago

Yes, I just tried it, and the result is similar.

{"draw":"2","error":"int() argument must be a string or a number, not 'NoneType'","recordsFiltered":"322687","recordsTotal":"322687"}

Alkasih commented 5 years ago

Any idea, please? I am still struggling find the solution for this. Could it because it may never work with Flask?

Pegase745 commented 5 years ago

It sure works as you can check the example flask_tut in the examples folder and on branch v2 if you want a newer version. You should use pdb and debug, I can't help more blindlessly

Alkasih commented 5 years ago

Happy New Year. How to upgrade the new version?

tdamsma commented 5 years ago

The new version is not released on pypi yet, so you can install it by cloning the right branch to your local machine and run python setup.py ..

tdamsma commented 5 years ago

Closed old issue