Skip to main content

Database aware widgets for PySide2

Project description

dbmodel

Creates an object representation of given database. Aim is to generate data aware pyside2 widgets with master detail relationships defined automatically from database definitions.

  • DBCombobox is implemented.
  • DBTableWidget is partly implemented.
  • Postgresql and SQLite databases are implemented.

Extracts column information, primary keys, and foreign keys if exists.

db = DBSQLite("test.db")
db.extract()

From that point, db has dictionary of tables, table names being the key value. A table has a dictionary of columns, column names being the key value.

If a foreign key is defined for any column, the referring table name is foreign_key_table, referring column name is foreign_key_column as attributes for column object.

Schema of the test database

CREATE TABLE city (
   id INTEGER NOT NULL, 
   name VARCHAR, 
   PRIMARY KEY (id)
);

CREATE TABLE district (
   id INTEGER NOT NULL, 
   name VARCHAR, 
   city_id INTEGER, 
   PRIMARY KEY (id), 
   FOREIGN KEY(city_id) REFERENCES city (id)
);

Line By Line

The lines below are from testapp.py MainWindow init section:

First, create the database object by giving connection parameters. For sqlite, file name is sufficient.

self.db = DBSQLite("test.db")

To create the table objects inside database object, call extract()

self.db.extract()

Place combobox widget for city table. Here, self.widget1 is the placeholder on ui definition. self.db is the database, "city" is the name of the table, "name" is the column for displaying inside combobox. 34 is the optional default value of the primary key for the combobox to display first.

self.city = DBComboBox(self.widget1, self.db, "city", "name", "id", 34)

Place the combobox for "district" table. Similar parameters as above.

self.district = DBComboBox(self.widget2, self.db, "district", "name", "id")

Now we will create a master detail connection between these two comboboxes. We will call detail table's setMaster method. self.city is the widget to use as master, "city_id" is the column name on the detail table, in our example, "district" table's "city_id" column.

self.district.setMaster(self.city, "city_id")

With three lines, we have two comboboxes connected with master-detail relationship.

Below is the line for creating a DBTableWidget for "district" table.

self.districtlist = DBTableWidget(self.widget3, self.db, "district")

This line connects "district" dbtablewidget to city combobox:

self.districtlist.setMaster(self.city, "city_id")

This line will create a dbtablewidget for "city" table.

self.citylist = DBTableWidget(self.widget4, self.db, "city")

This line connects "district" dbtablewidget to "city" dbtablewidget:

self.districtlist.setMaster(self.citylist, "city_id")   

Demo

video

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbwidgets-0.0.1.tar.gz (6.1 kB view details)

Uploaded Source

File details

Details for the file dbwidgets-0.0.1.tar.gz.

File metadata

  • Download URL: dbwidgets-0.0.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for dbwidgets-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f1987e8c9fd1d3925fb9312b208a4d7270906e166fb2bcef342bcaecf0cfb53a
MD5 a92415379e49cb42167e39f2d95826fa
BLAKE2b-256 060084aaad29db17f9b5d20d7d5eda261ede7a9b509de602265d24430dd5307b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page