0

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 a good way to do it?

marue
  • 5,588
  • 7
  • 37
  • 65
  • Can you explain how your question is different from [this one](http://stackoverflow.com/questions/402217/how-to-store-a-dictionary-on-a-django-model)? – Tim Post Feb 24 '11 at 14:59
  • I think it is mostly in the type of data that needs to be stored. I just try to save some user preferences i do not need to be models. They don't have to be searchable. It's more like a "session-freeze". The accepted answer in that post you linked to is not satisfying as an answer to this one. – marue Feb 24 '11 at 15:45

2 Answers2

1

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.

Stephen Paulger
  • 5,204
  • 3
  • 28
  • 46
0

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.

Carlton Gibson
  • 7,278
  • 2
  • 36
  • 46