QSqlRelationalTableModel

Inheritance diagram of QSqlRelationalTableModel

Synopsis

Functions

Virtual functions

Detailed Description

The PySide.QtSql.QSqlRelationalTableModel class provides an editable data model for a single database table, with foreign key support.

PySide.QtSql.QSqlRelationalTableModel acts like PySide.QtSql.QSqlTableModel , but allows columns to be set as foreign keys into other database tables.

../../_images/noforeignkeys.png ../../_images/foreignkeys.png

The screenshot on the left shows a plain PySide.QtSql.QSqlTableModel in a PySide.QtGui.QTableView . Foreign keys (city and country ) aren’t resolved to human-readable values. The screenshot on the right shows a PySide.QtSql.QSqlRelationalTableModel , with foreign keys resolved into human-readable text strings.

The following code snippet shows how the PySide.QtSql.QSqlRelationalTableModel was set up:

model.setTable("employee")

model.setRelation(2, QSqlRelation("city", "id", "name"))
model.setRelation(3, QSqlRelation("country", "id", "name"))

The PySide.QtSql.QSqlRelationalTableModel.setRelation() function calls establish a relationship between two tables. The first call specifies that column 2 in table employee is a foreign key that maps with field id of table city , and that the view should present the city ‘s name field to the user. The second call does something similar with column 3.

If you use a read-write PySide.QtSql.QSqlRelationalTableModel , you probably want to use PySide.QtSql.QSqlRelationalDelegate on the view. Unlike the default delegate, PySide.QtSql.QSqlRelationalDelegate provides a combobox for fields that are foreign keys into other tables. To use the class, simply call QAbstractItemView.setItemDelegate() on the view with an instance of PySide.QtSql.QSqlRelationalDelegate :

view =  QTableView()
view.setModel(model)
view.setItemDelegate(QSqlRelationalDelegate(view))

The sql/relationaltablemodel example illustrates how to use PySide.QtSql.QSqlRelationalTableModel in conjunction with PySide.QtSql.QSqlRelationalDelegate to provide tables with foreigh key support.

../../_images/relationaltable.png

Notes:

  • The table must have a primary key declared.
  • The table’s primary key may not contain a relation to another table.
  • If a relational table contains keys that refer to non-existent rows in the referenced table, the rows containing the invalid keys will not be exposed through the model. The user or the database is responsible for keeping referential integrity.
  • If a relation’s display column name is also used as a column name in the main table, or if it is used as display column name in more than one relation it will be aliased. The alias is is the relation’s table name and display column name joined by an underscore (e.g. tablename_columnname). All occurrences of the duplicate display column name are aliased when duplication is detected, but no aliasing is done to the column names in the main table. The aliasing doesn’t affect PySide.QtSql.QSqlRelation , so QSqlRelation.displayColumn() will return the original display column name, but QSqlRecord.fieldName() will return aliases.
  • When using PySide.QtSql.QSqlRelationalTableModel.setData() the role should always be Qt.EditRole , and when using PySide.QtSql.QSqlRelationalTableModel.data() the role should always be Qt.DisplayRole .

See also

PySide.QtSql.QSqlRelation PySide.QtSql.QSqlRelationalDelegate Relational Table Model Example

class PySide.QtSql.QSqlRelationalTableModel([parent=None[, db=QSqlDatabase()]])
Parameters:

Creates an empty PySide.QtSql.QSqlRelationalTableModel and sets the parent to parent and the database connection to db . If db is not valid, the default database connection will be used.

PySide.QtSql.QSqlRelationalTableModel.relation(column)
Parameters:columnPySide.QtCore.int
Return type:PySide.QtSql.QSqlRelation

Returns the relation for the column column , or an invalid relation if no relation is set.

PySide.QtSql.QSqlRelationalTableModel.relationModel(column)
Parameters:columnPySide.QtCore.int
Return type:PySide.QtSql.QSqlTableModel

Returns a PySide.QtSql.QSqlTableModel object for accessing the table for which column is a foreign key, or 0 if there is no relation for the given column .

The returned object is owned by the PySide.QtSql.QSqlRelationalTableModel .

PySide.QtSql.QSqlRelationalTableModel.setRelation(column, relation)
Parameters:

Lets the specified column be a foreign index specified by relation .

Example:

model.setTable("employee")

model.setRelation(2, QSqlRelation("city", "id", "name"))

The PySide.QtSql.QSqlRelationalTableModel.setRelation() call specifies that column 2 in table employee is a foreign key that maps with field id of table city , and that the view should present the city ‘s name field to the user.

Note: The table’s primary key may not contain a relation to another table.