AttributeError: ‘QDateTime’ object has no attribute ‘upper’


Traceback (most recent call last):
File "D:\port\workspace\python\PyQtTest\hris\mdi00\querymodel.py", line 61, in data
return value.upper()
AttributeError: 'QDateTime' object has no attribute 'upper'

The code:

class CustomSqlModel(QSqlQueryModel):
  def data(self, index, role):
    value = super(CustomSqlModel, self).data(index, role)
    if value is not None and role == Qt.DisplayRole:
      if index.column() == 0:
        return '#%s' % value
      elif index.column() == 2:
        return value.upper()          

    if role == Qt.TextColorRole and index.column() == 1:
      return QColor(Qt.blue)

    return value

The problem is at line 8: “return value.upper()”.
I edit the class with adding function hasattr(value, ‘upper’),

class CustomSqlModel(QSqlQueryModel):
  def data(self, index, role):
    value = super(CustomSqlModel, self).data(index, role)
    if value is not None and role == Qt.DisplayRole:
      if index.column() == 0:
        return '#%s' % value
      elif index.column() == 2:
        if hasattr(value, 'upper'):
          return value.upper()

    if role == Qt.TextColorRole and index.column() == 1:
      return QColor(Qt.blue)

    return value

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: