Hi,
I have several javascript charts where users can choose different metrics and timelines etc. I want to store these selections so that when they return to the app the charts render from the previous selections
I’m looking at using Django session data stored within the database, but I’m not 100% sure this is appropriate for my use case, as I want the data to render after logging out of their session, and then logging in again.
What is the best way to do this, should I just store the selections in the database in a separate model, retrieve this when the user logs in, and also update every time the metrics are changed?
The data that renders the chart is in JSON
. It works fine as it is except for the expiry of the session so ideally I do not want to change the structure of the data being saved as I have a consistent format across all my charts
Thanks
Tom
Ha thanks, Ken, that was a quick reply.
Would I just store the data as a textfield
? or is there a better way? This seems like a stupid question.
That depends. Personally, I’d be more inclined to either:
- Store it as a set of columns that correspond to how your views need it.
- Store it as a JSON object.
It really isn’t - but it is more a question that the representation depends upon exactly what data you need to save and its format - and what you’re going to need to do to “restore” it from the database for use.
Well at the moment I just call a view from my javascript that returns the session data that returns a JSON object.
How would i store as a JSON object?
At the moment i am, first setting the session data using
request.session['chart_data'] = response_data
response_data
is the data contains the data for the chart which includes the select metrics (date range, metric, projects)
Then when the user logs in the javascript makes the call and returns the saved session data. (if the session hasn’t expired)
So i assume instead of getting the session data. I would just do something like
def user_selections(request, user_id):
try:
user_data = UserData.objects.values_list('user_selections', flat=True).get(pk=user_id)
...
where user_selections
would be raw JSON in text or a JSON object?
Just saw there is an option for JSONField
Didn’t know this existed. I’ll give this a go.
Thanks again, Ken.
Tom.
Instead of storing it in session, store it as a JSONField in a model.
Assuming each user is only going to have one entry, then your model could be something like this:
class ChartData(models.Model):
user = OneToOneField(User, ...)
chart = JSONField()
saving it is:
ChartData.objects.update_or_create(user=request.user, chart = response_data)
and retrieving it is:
response_data = ChartData.objects.get(user=request.user).chart
Amazing. Thanks. Ken.
If i was to expand this to multiple charts, would you create a field for each chart?
Like this
class ChartData(models.Model):
user = OneToOneField(User, ...)
chart1 = JSONField()
chart2 = JSONField()
...
and then ChartData.objects.update_or_create(user=request.user, chart1 = response_data1)
chart_data = ChartData.objects.get(user=request.user).chart1
Multiple charts on one page, or multiple pages?
Bounded number of charts or unbounded?
That’s going to affect the decision.
Not sure what this means?
I have a fixed number if that’s what you mean? and yes multiple(3)on 1 page. I have a dashboard as the landing zone for users where the charts are presented.
Bounded basically means “limited” in this case. For example, you might say that you can have up to 8 charts on a page - that’s the “bounds” for the page.
But yes, if it’s a fixed (or limited) number on a page, you could do separate fields.
Or, you could do it as a nested dict, where a “chart ID” is the key, and the chart specification itself is the value.
e.g.:
chart_data = {
'chart_1': response_data_1,
'chart_2': response_data_2,
'chart_3': response_data_3,
}
Got it! Thanks, Ken. Ill give this a go.