What is the right way to use PostgreSQL with PyQt5

Dennis Jensen djensen at pgcontrols.com
Thu Jun 11 14:26:24 BST 2020


It sounds like Steve might be doing pretty much what I am talking about 
as well -- but to re-iterate and to break it down a bit more --

PyQt-GUI-Program -- that displays data to the users in whatever viewing 
manner the programmer wishes to use (this is a front-end)

Python-Router (if needed) -- this handles requests from the (front-end) 
and send it to the appropriate back-end (if more than one)

Python-Querier -- this is a back-end that interacts with whatever 
database you are using

---- 
OpensConnect-SendsRequest-GetsResults-ClosesConnect-ConverstResultsToPythonDictionary-SendsConvertedResultsToRouter

So there is no need for any PyQT direct interaction with the database 
and in fact that would be over-complicating the program further it 
appears there would be no driver issues either and you are NOT inventing 
a new MVC (or M-C-V) you are just using the concept and completely 
divorcing your front-end from your back-end to keep them autonomous 
which helps in many many ways to make your code much more solid-state as 
well as helps with maintainability and efficiency. Remember K.I.S.S. It 
is better than M.I.C.C. It --- (Keep It Simple and Smart) vs (Make It 
Complex and Crappy pronounced Miss)

On 6/10/2020 6:34 PM, Stephen Waterbury wrote:
>
> 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/20200611/8823bb38/attachment-0001.htm>


More information about the PyQt mailing list