0

I have the following code:

query = self.dbObj.session.query(MaterialsTable)
rowCounter = self.dbObj.session.query(MaterialsTable)

for attr , val in interfaceCodesObject.filterNameDict.items():
    query = query.filter(and_(getattr(MaterialsTable,attr).like("%%%s%%" % val)))
    rowCounter = rowCounter.filter(and_(getattr(MaterialsTable,attr).like("%%%s%%" % val))).count()
query.all()

When i use mutiple field, i get the following traceback:

Traceback (most recent call last):
  File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFindFrame.py", line 202, in <lambda>
    QtCore.QObject.connect(self.pushButtonSearch, QtCore.SIGNAL(_fromUtf8("clicked()")), lambda: self.interfaceCodesConstructor.responseToRequestForData(self))
  File "/home/mohsen/codes/amlak/amlak/src/ui/interface/interface.py", line 109, in responseToRequestForData
    self.materialsObejct.findData(self.objectSearchMaterials,self)
  File "/home/mohsen/codes/amlak/amlak/src/materials/materials.py", line 133, in findData
    rowCounter = rowCounter.filter(and_(getattr(MaterialsTable,attr).like("%%%s%%" % val))).count()
AttributeError: 'long' object has no attribute 'filter'

But when i use just one field it use rowCounter as integer and real return.

My question is , how can i adapt it with multiple field?Such as : Where name = 'barak' and id != 3 But one filed such as : Where name = 'barak'

4

1 回答 1

3

你只想得到最终结果的计数吗?实际上,在这种情况下,您只需要一个query对象而不是两个queryand rowCounter

query = self.dbObj.session.query(MaterialsTable)
for attr , val in interfaceCodesObject.filterNameDict.items():
    query = query.filter(and_(getattr(MaterialsTable,attr).like("%%%s%%" % val)))

# all result rows
query.all()

# count of rows
rowCounter = query.count()

希望这可以帮助。

于 2013-09-13T05:04:44.430 回答