sql - Efficient Pagination from Join in Django -
I have a problem here and with a paging and paging. I have 3 models:
- Tag
- TagRelation
I betweent many relationships to manage tag management Now with the pad and tag table (with the feature of te ManyToMany field), when I am in the pad and tag table, I give something like this ...
Pad.name tag.name etc ------------ --------------- Pad 1 tag 1 ... Pad 1 tag 2. .. .pad2 tag3 ...
Now when I create a paginator () object from these results it's clearly incorrect per page Items count I must iterate through all the results and thus generate a dictionary ... [{name: 'pad1', tag: ['tag1', 'tag2']}, {name: 'pad 2 '....]
... and use the pgnitor on it to get the correct page.
What is the best way to deal with this problem? I can do a DB query for each pad object on the current page, so that he can get the tag, but I think the server must kill sooner or later (should be presenting).
Content may also result in a lot of system memory costs (or will it expose me :) for a very long time and running through the whole set :)
If I understand correctly, padding you Paginate on the packet, then use both methods to properly drag an appropriate tag on the tagrellation object, and use that in your view / template, the data is something like this:
O.D. in thispagepadids = [mypageobject.object_list] Tagrelals = tagarelation.obages.philter (pad__ id__in = thispagetagids) .select_related ('tag', 'pad'))
(Suppose you have your page object in mypageobject). You can get the pad and tag for any tangac in the code (perhaps the simplest way to use the template tag) but DB only performs one (giant) query, and it is still correct to point your page.
Note that we had to do 2 questions because you can not use select_related directly on multiple-to-many areas (see), but you can use it from both intermediate M2M tablets From to FK can follow. .
Comments
Post a Comment