QSqlRelationalTableModelclass provides an editable data model for a single database table, with foreign key support. 更多 …
def
relation
(column)
def
setJoinMode
(joinMode)
def
relationModel
(column)
def
setRelation
(column, relation)
QSqlRelationalTableModel举动像QSqlTableModel, but allows columns to be set as foreign keys into other database tables.
![]()
![]()
The screenshot on the left shows a plain
QSqlTableModel在QTableView. Foreign keys (cityandcountry) aren’t resolved to human-readable values. The screenshot on the right shows aQSqlRelationalTableModel, with foreign keys resolved into human-readable text strings.The following code snippet shows how the
QSqlRelationalTableModelwas set up:model.setTable("employee") model.setRelation(2, QSqlRelation("city", "id", "name")) model.setRelation(3, QSqlRelation("country", "id", "name"))
setRelation()function calls establish a relationship between two tables. The first call specifies that column 2 in tableemployeeis a foreign key that maps with fieldidof tablecity, and that the view should present thecity‘snamefield to the user. The second call does something similar with column 3.If you use a read-write
QSqlRelationalTableModel, you probably want to useQSqlRelationalDelegateon the view. Unlike the default delegate,QSqlRelationalDelegateprovides a combobox for fields that are foreign keys into other tables. To use the class, simply callsetItemDelegate()on the view with an instance ofQSqlRelationalDelegate:view = QTableView() view.setModel(model) view.setItemDelegate(QSqlRelationalDelegate(view))relationaltablemodel example illustrates how to use
QSqlRelationalTableModelin conjunction withQSqlRelationalDelegateto provide tables with foreign key support.![]()
注意事项:
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 relational table, or if it is used as display column name in more than one relation it will be aliased. The alias is the relation’s table name, display column name and a unique id joined by an underscore (e.g. tablename_columnname_id).
fieldName()will return the aliased column name. 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 affectQSqlRelation,因此displayColumn()will return the original display column name.The reference table name is aliased. The alias is the word “relTblAl” and the relationed column index joined by an underscore (e.g. relTblAl_2). The alias can be used to filter the table (For example,
setFilter(“relTblAl_2=’Oslo’ OR relTblAl_3=’USA’”)).当使用
setData()the role should always beEditRole, and when usingdata()the role should always beDisplayRole.另请参阅
QSqlRelationQSqlRelationalDelegateRelational Table Model Example
QSqlRelationalTableModel
(
[
parent=None
[
,
db=QSqlDatabase()
]
]
)
¶
- param parent
QObject- param db
Creates an empty
QSqlRelationalTableModel
and sets the parent to
parent
and the database connection to
db
。若
db
is not valid, the default database connection will be used.
PySide2.QtSql.QSqlRelationalTableModel.
JoinMode
¶
|
常量 |
描述 |
|---|---|
|
QSqlRelationalTableModel.InnerJoin |
|
|
QSqlRelationalTableModel.LeftJoin |
|
另请参阅
PySide2.QtSql.QSqlRelationalTableModel.
relation
(
column
)
¶
column
–
int
Returns the relation for the column
column
, or an invalid relation if no relation is set.
另请参阅
PySide2.QtSql.QSqlRelationalTableModel.
relationModel
(
column
)
¶
column
–
int
返回
QSqlTableModel
object for accessing the table for which
column
is a foreign key, or
None
if there is no relation for the given
column
.
The returned object is owned by the
QSqlRelationalTableModel
.
另请参阅
PySide2.QtSql.QSqlRelationalTableModel.
setJoinMode
(
joinMode
)
¶
joinMode
–
JoinMode
Sets the SQL
joinMode
to show or hide rows with NULL foreign keys. In
InnerJoin
mode (the default) these rows will not be shown: use the
LeftJoin
mode if you want to show them.
另请参阅
JoinMode
PySide2.QtSql.QSqlRelationalTableModel.
setRelation
(
column
,
relation
)
¶
column
–
int
relation
–
QSqlRelation
Lets the specified
column
be a foreign index specified by
relation
.
范例:
model.setTable("employee")
model.setRelation(2, QSqlRelation("city", "id", "name"))
The 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.
另请参阅