What is the right way to use PostgreSQL with PyQt5

Stephen Waterbury waterbug at pangalactic.us
Thu Jun 11 00:34:07 BST 2020


You don't need to "invent a new MVC", just use the MVC in PyQt:
QTableView is usable with any type of model that can be built using,
for example, QAbstractTableModel.  I'm attaching some example code
that has an "ODTableModel" for which the "model" is a list of OrderedDict
instances, and an "ObjectTableModel" that subclasses ODTableModel to
use arbitrary objects, which could be, for example, sqlalchemy objects.
You write your own code to do the sqlalchemy interactions with the
database, and just call them from the ObjectTableModel ...
that is one easy way to apply the PyQt MVC with an ORM back-end.
This example code is based on code I use in my app, which does
exactly what I describe.

You would want to create a subclass of QTableView that could be
called "ObjectTableView" that takes a list of object instances and a "view"
(a list of the names of the attributes that you want to display).  That's
where you could create, say, a context menu that gives you various
options like to delete an object, etc.  Again, the objects can be sqlalchemy
objects, and you can do db operations using them (sqlalchemy objects are
basically always in a transaction, which you can use to do operations and
commits, etc.).

Steve

On 6/10/20 5:42 PM, Nenad Lamza wrote:

> Thanks Sibylle, do you know how much work it takes, inventing 
> completely new MVC? I haven't found any example of it (SQLAlchemy, 
> psycopg2, QTableView, (QDataWidgetMapper) together) on the web. Can 
> you put any link. It is much more practical and logical to use PyQt 
> Sql classes with QTableView. I avoid QAbstractItemModel and use it 
> only for very very special cases. It is so sad that Qt and PyQt 
> haven't documented what PostgreSQL drivers and what version of them we 
> should use with particular Qt/PyQt versions. And because of that you 
> suggest me to turn the upside down the whole Qt MVC framework. I won't 
> do that. The app with standard PyQt MVC, QSqlDatabase, QSqlQuery, 
> QSqlQueryModel, QSqlQueryModel,..., QTableView works so great that I 
> think it is easier to (randomly) find the right combination of drivers 
> than write the whole new framework. Nenad Am 10.06.2020 um 14:34 
> schrieb Nenad Lamza:
>> Thanks Barry (and also Dennis) to your answers.
>>
>> So, I have a choice:
>>
>> 1. Use standard PyQt MVC, QSqlDatabase, QSqlQuery, QSqlQueryModel,
>> QSqlQueryModel, QTableView,..., but without ORM like SqlAlchemy, and
>> potentially have problems with PostgreSQL drivers
>>
>> 2. Use other PostgreSQL drivers like psycopg2 and don't use PyQt MVC and
>> Sql classes and use ORM like SqlAlchemy with business logic and objects.
>> Isn't the whole point of (Py)Qt MVC to use all those classes I
>> mentioned? I can't imagine in that case (without PyQt Sql classes and
>> using SqlAlchemy) how would I present data to the user in eg. QTableView
>> or some other GUI table on screen? Building my own MVC (it takes years)?
>>
> You can use the Qt Model-View classes, just without QtSql. Instead you
> could create your own Model class, subclassed from QAbstractItemModel or
> use a QStandardItemModel (or subclass that). And put your data, got via
> SQLAlchemy or directly from psycopg2, into that model. QTableView and
> QDataWidgetMapper can use that just as well as the QSql...Model classes.
>
> HTH
> Sibylle


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20200610/7c402255/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: custom_tablemodels.py
Type: text/x-python
Size: 7015 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20200610/7c402255/attachment-0001.py>


More information about the PyQt mailing list