Identifying selection from dropdownlist for use in identifying relationships between tables

I am developing a dashboard app where all the objectives, results, etc have one dashboard as the FK. Right now it’s just 1 dashboard, but I want a user to be able to select a dashboard from a list and the selected dashboard’s id be saved so I can filter all the related objects when displaying them.

For example, after logging in the user would be directed to a drop down style list to select the dashboard to display/update; and the dashboard id would be used in each view unit the user logs out. User.id won’t work because a user could have a number of dashboards associated with the id.

Are there any good examples or documentation to help me figure this out?

The easiest way to handle this would be to store the dashboard id in the user’s session. See How to use sessions | Django documentation | Django, particularly the section “Using sessions in views”.

If I figured it out properly, using this code:

        n=1 # Dashboard id number returned from button_id in GET data - will be set in final using request.GET()
        request.session['dashboard_id'] = n  #assign dashboard_id the value n

will assign the value selected from the list in the session, and I should do that in the first view called after selecting the dashboard.

I can retrieve the active dashboard with:

        dashboard_id = request.session.get('dashboard_id') # This how you retrieve the id in any view

and use that to filter results with dashboard.id = dashboard_id or simply use:

dashboard.id = request.session.get('dashboard_id')

Oddly, when I look at the sessions table in my DB it has no column dashboard_id; although I have enabled sessions per the documentation.

If you’re using the database session store, the session data is stored in the session as a JSON data object within the session_data field. This allows Django to store all the data for a session in one row of the database.

Personally I think storing it in the session is almost always the wrong thing to do. It means your app forgets what the user was doing after each login, and between devices. I always find that frustrating when apps do that. I would add a field on your custom user/profile model for it.