# Buggy code
def _get_base_query(
    self, query=None, filters=None, order_column="", order_direction=""
):
    if filters:
        query = filters.apply_all(query)
    if order_column != "":
        # if Model has custom decorator **renders('<COL_NAME>')**
        # this decorator will add a property to the method named *_col_name*
        if hasattr(self.obj, order_column):
            if hasattr(getattr(self.obj, order_column), "_col_name"):
                order_column = getattr(getattr(self.obj, order_column), "_col_name")
        query = query.order_by("%s %s" % (order_column, order_direction))
    return query


# Traceback (TypeError)
Sorry, something went wrong

500 - Internal Server Error

Stacktrace

Traceback (most recent call last):
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/superset/.env/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/security/decorators.py", line 26, in wraps
return f(self, *args, **kwargs)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/views.py", line 450, in list
widgets = self._list()
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/baseviews.py", line 870, in _list
page_size=page_size)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/baseviews.py", line 786, in _get_list_widget
count, lst = self.datamodel.query(joined_filters, order_column, order_direction, page=page, page_size=page_size)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/models/sqla/interface.py", line 103, in query
order_direction=order_direction)
File "/opt/superset/.env/lib/python2.7/site-packages/flask_appbuilder/models/sqla/interface.py", line 67, in _get_base_query
query = query.order_by(order_column + ' ' + order_direction)
TypeError: coercing to Unicode: need string or buffer, NoneType found

# Fixed code
def _get_base_query(
    self, query=None, filters=None, order_column="", order_direction=""
):
    if filters:
        query = filters.apply_all(query)
    if order_column != "":
        # if Model has custom decorator **renders('<COL_NAME>')**
        # this decorator will add a property to the method named *_col_name*
        if hasattr(self.obj, order_column):
            if hasattr(getattr(self.obj, order_column), "_col_name"):
                order_column = getattr(self._get_attr(order_column), "_col_name")
        if order_direction == "asc":
            query = query.order_by(self._get_attr(order_column).asc())
        else:
            query = query.order_by(self._get_attr(order_column).desc())
    return query