Frage

I am having the following code below based on the PyQt documentation:

  model = QSqlRelationalTableModel()
  model.setTable("employee")
  model.setRelation(2, QSqlRelation("city", "id", "name"))
  model.setRelation(3, QSqlRelation("country", "id", "name"))

The third line will bring all cities to the relation. How can i filter the relation, and not the model itself? For example, I would like cities have the name ('X').

War es hilfreich?

Lösung

You must filter the model referenced by the relation, but you can invoke it directly; in your case:

    model.relationModel(2).setFilter("city like 'x'");

AFAIK, the filter is affecting a "copy" of the table (a model) that was instantiated in this relation and it does not affect any other instances of the same table.

In fact, you could have something like this:

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

    model.relationModel(2).setFilter("city like 'x'");
    model.relationModel(4).setFilter("city like 'y'");

relation (2) and (4) point to the same table, but they have two different models, each one with its own filter.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top