setEditStrategy AttributeError: type object 'QSqlTableModel' has no attribute 'OnFieldChange'
-
Hello,
There are many examples here in the forum that it works like this:filename = os.path.join(os.path.dirname(__file__), "telefonnummern_verwaltung.db") db = QSqlDatabase.addDatabase('QSQLITE') db.setDatabaseName(filename) if db.open(): self.table_model = QSqlTableModel() self.table_model.setTable("telefon_verwaltung") self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange) self.table_model.select()
But I get this error message:
self.table_model.setEditStrategy(QSqlTableModel.OnFieldChange)
AttributeError: type object 'QSqlTableModel' has no attribute 'OnFieldChange'What am I doing wrong?
-
@PythonQTMarlem
Goodness knows, it looks correct to me, this is Python/PySide.... Can you maybe press F2 key in Creator onQSqlTableModel
and it takes you to wherever the PySide class definition is so you can look around to see ifOnFieldChange
is there? -
@JonB Thank you for your answer.
I don't use the qt-creator. I code qt in PyCharm.In my imports I have just PyQt6. No PySide.
can I also implement your idea without qt Creator?
-
Hi,
Can you try with
QSqlTableModel.EditStrategy.OnFieldChange
? -
@PythonQTMarlem
Well whatever the key is in PyCharm then. To let you browse definitions.
Try @SGaist's suggestion. -
@SGaist Thank you.
Your code doesn't produce an error message.
This code also does not generate an error message:QSqlTableModel.EditStrategy.OnFieldChange
or this Code:
self.table_model.EditStrategy.OnFieldChange
But when I change a column in a record. I then go to another column in the same data set and then exit the program. Then my change was not saved.
When I change the record, then my change was saved.
-
@PythonQTMarlem
Well at least that has resolved the Python issue, thanks to @SGaist's correct guess.Try with
OnManualSubmit
and an explicitsubmitAll()
(e.g. from a button, and check its return result). Find out whether it's actuallyOnFieldSubmit
or updating in general which is at issue. -
I have the solution:
self.table_model.setEditStrategy(QSqlTableModel.EditStrategy.OnFieldChange)look here:
https://www.tutorialspoint.com/pyqt/pyqt_database_handling.htmThank you for your help!