join two querysets

Skip to first unread message

Jul 23, 2014, 11:03:26 AM7/23/14
Sorry to bother but I ran out of ideas.
If I have two query sets of same type and each has different items - can I somehow join them? (intersection of items).

I am trying to make some complicated select - I have table Asset with FK to other tables - thats fine I can filter like this:
QDjangoQuerySet<Asset> categoryAssets;
= assets.selectRelated().filter(QDjangoWhere("category__name", QDjangoWhere::Equals, "movies"));

But then I have the opposite - some table (File) with FK to table Asset - here I want to match another assets. In this case I will have to start from QDjangoQuerySet<File> and get asset ids. Then filter the assets ids into QDjangoQuerySet<Asset>
QDjangoQuerySet<File> files;
= files.selectRelated().filter(QDjangoWhere("name", QDjangoWhere::Equals, "something"));
foreach(..) { get asset ids into listOfIds }
QDjangoQuerySet<Asset> fileAssets;
= fileAssets.magicalyFilterByds(listOfIds);//I dont know how to do this

Then I have to join the two query sets:
QDjangoQuerySet<Asset> intersection = categoryAssets.intersect(fileAssets); //is there some mechanism like this?

I think this is not very good approach. There must be some better way.

Aug 27, 2014, 7:43:17 AM8/27/14
Ok I found something which moved me closer to solution.
It is possible to do a SELECT using "reverse" relation like this:

QDjangoQuerySet<Asset> assets;
= assets.filter(QDjangoWhere("file__name", QDjangoWhere::Equals, "filename.txt");

Here the file has FK to asset.
But this does not work if the other table is 2 "levels" away from Asset like:

QDjangoQuerySet<Asset> assets;
= assets.filter(QDjangoWhere("subtitletrack__language", QDjangoWhere::Equals, "eng");

The table subtitletrack has FK to file(there could be more subtitle streams in file) and file has FK to asset.
QDjango cannot find a connection from subtitletrack to asset.

My workaround is to add FK from subtitletrack to asset, but I am not very happy with this.
I was searching for how this works in django but didnt find anything.
Reply all
Reply to author
0 new messages