Re: What is the right way to use PostgreSQL with PyQt5
Dennis Jensen <[email protected]> Thu, 11 Jun 2020 08:26:24 -0500
| Newsgroups | gmane.comp.python.pyqt-pykde |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--------------8F732174B736927B5BA33755
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
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
>
>
--------------8F732174B736927B5BA33755
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>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
--</p>
<p>PyQt-GUI-Program -- that displays data to the users in whatever
viewing manner the programmer wishes to use (this is a front-end)</p>
<p>Python-Router (if needed) -- this handles requests from the
(front-end) and send it to the appropriate back-end (if more than
one)</p>
<p>Python-Querier -- this is a back-end that interacts with whatever
database you are using</p>
<p>----
OpensConnect-SendsRequest-GetsResults-ClosesConnect-ConverstResultsToPythonDictionary-SendsConvertedResultsToRouter</p>
<p>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)<br>
</p>
<div class="moz-cite-prefix">On 6/10/2020 6:34 PM, Stephen Waterbury
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:[email protected]">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<p><font face="Helvetica, Arial, sans-serif">You don't need to
"invent a new MVC", just use the MVC in PyQt:<br>
QTableView is usable with any type of model that can be built
using,<br>
for example, QAbstractTableModel. I'm attaching some example
code<br>
that has an "ODTableModel" for which the "model" is a list of
OrderedDict<br>
instances, and an "ObjectTableModel" that subclasses
ODTableModel to<br>
use arbitrary objects, which could be, for example, sqlalchemy
objects.<br>
You write your own code to do the sqlalchemy interactions with
the<br>
database, and just call them from the ObjectTableModel ...<br>
that is one easy way to apply the PyQt MVC with an ORM
back-end.<br>
This example code is based on code I use in my app, which does<br>
exactly what I describe.<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">You would want to
create a subclass of QTableView that could be<br>
called "ObjectTableView" that takes a list of object instances
and a "view"<br>
(a list of the names of the attributes that you want to
display). That's<br>
where you could create, say, a context menu that gives you
various<br>
options like to delete an object, etc. Again, the objects can
be sqlalchemy<br>
objects, and you can do db operations using them (sqlalchemy
objects are<br>
basically always in a transaction, which you can use to do
operations and<br>
commits, etc.).<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">Steve<br>
</font></p>
<p><font face="Helvetica, Arial, sans-serif">On 6/10/20 5:42 PM,
Nenad Lamza wrote: </font></p>
<blockquote type="cite"><font face="Helvetica, Arial, sans-serif">
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<pre class="moz-quote-pre" wrap=""><div class="moz-txt-sig">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:
</div></pre>
<blockquote type="cite" style="color: #000000;">
<pre class="moz-quote-pre" wrap="">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)?
</pre>
</blockquote>
<pre class="moz-quote-pre" wrap="">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</pre>
</font></blockquote>
<font face="Helvetica, Arial, sans-serif"> <br>
</font>
<p><br>
</p>
</blockquote>
</body>
</html>
--------------8F732174B736927B5BA33755--