Best way to keep the current selection

13 views
Skip to first unread message

Lee Hinde

unread,
Jul 26, 2016, 9:02:59 PM7/26/16
to django...@googlegroups.com
I'm wondering if there's a better way to maintain a selection of records across calls to the app.

In one of my projects users can do ad-hoc searches; i.e., Last Name = Smith, Class Name = Yoga, etc.

They do the search, then get the query results presented in a table.

Then they can do things with the selection like run reports or make it the basis for further queries. 

To maintain state, after any ad-hoc query, I store the ids for the found records:

self.request.session['query.classes.last_ids'] = list(queryset.values_list('id', flat=True))

It works fine and I've not had any problems I can lay to this. But I'm executing the query at least twice and I'm storing, potentially, thousands of ids in the session cache.

The only other alternative I've thought of was to store the query criteria and re-execute it, but a) that's complicated and b) the underlying results could change while the user is deciding on the next step.

Is there a best practice for this sort of thing?

Thanks.

Javier Guerra Giraldez

unread,
Jul 27, 2016, 4:34:53 AM7/27/16
to django...@googlegroups.com
On 27 July 2016 at 02:02, Lee Hinde <leeh...@gmail.com> wrote:
> To maintain state, after any ad-hoc query, I store the ids for the found records:
>
> self.request.session['query.classes.last_ids'] = list(queryset.values_list('id', flat=True))

add a new model: Previous_Searchs or something like this, with
metadata on the search (user, date, sort critera, etc) and a many2many
link to the found records.

it makes it easy to repeat a search, keep it for longer, name it, or
even share it


--
Javier
Reply all
Reply to author
Forward
0 new messages