How can i store user preferences as a dictionary to a user profile?
I have a list of categories, each of which has a metacategory. I then create some select multiple fields to allow a user to filter which objects he wants to see. This gives me a dictionary like this:
filter={'genres':[id1,id2...],'parts':[id9,id11...],...}
Now i want to store that to the user profile, so the user immediately gets his last search results when returning to the page.
I know i could do this with a M2M field, but that would involve to recreate the filer dictionary, so i would prefer to store the dictionary. Is this a bad idea? Why? If not, what would be开发者_Python百科 a good way to do it?
As already mentioned pickling is one option. Personally I would choose a JSONfield (you can find them easily just Google) as the format is more readable which can be nicer for debugging.
It's worth pointing out that it's hard to query models based on data stored in dictionaries. It looks like that won't be an issue for you though.
First option is to just pickle the dictionary and store that. For this you can use django-picklefield.
I might be inclined to stick this in a Cookie though: it's not sensitive (I assume), the user may never return, it's not something that I need to store.
精彩评论