Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Is storing objects in session a good practice?

class Book(models.Model):
  author = models.ForeignKey(User)
  name = models.CharField(max_length=100)

def view(request):
  book = Book.objects.get(pk=1)
  request.session['selected_book'] = book

Is it a good practice to store Objects in Session instead of their id ?
Will it be "picklable" enough to be used in templates for example ?

<div>{{ request.session.book.author.name }}</div>
like image 757
Pierre de LESPINAY Avatar asked May 10 '12 10:05

Pierre de LESPINAY


1 Answers

This seems like a bad idea. Apart from anything else, if you store an object in the session, it won't change if/when the database version does.

like image 101
Daniel Roseman Avatar answered Oct 22 '22 02:10

Daniel Roseman