join two querysets

61 views
Skip to first unread message

otop...@gmail.com

unread,
Jul 23, 2014, 11:03:26 AM7/23/14
to qdj...@googlegroups.com
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;
matched
= 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;
matched
= files.selectRelated().filter(QDjangoWhere("name", QDjangoWhere::Equals, "something"));
foreach(..) { get asset ids into listOfIds }
QDjangoQuerySet<Asset> fileAssets;
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.
Thanks.

otop...@gmail.com

unread,
Aug 27, 2014, 7:43:17 AM8/27/14
to qdj...@googlegroups.com
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;
matched
= 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;
matched
= 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
Forward
0 new messages