Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Add model object to cookie in Django

I have a model

class Journals(models.Model):
    JOURNAL_ID = models.AutoField(primary_key=True)
    Journal_name = models.CharField(max_length=255)
    Journal_slug = models.SlugField(max_length=255)

which I am trying to add to a cookie and then retrieve in other views. I am doing this as follows

journal = request.COOKIES.get('journal')

if journal:
    from_cookie = True
else:
    journal = get_object_or_404(Journals, Journal_slug=journal_slug)
    from_cookie = False

template = [...]

template_context = {
    ...
    'journal': journal,
    'from_cookie': from_cookie,
    ...
}

response = render(request, template, template_context)
response.set_cookie('journal', value=journal)
return response

It seems however that only the Journal_name is added to the cookie rather than the full object. This way I end up having no access to any of its other fields. How do I get so that the full journal object is added and retrieved instead?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

First of all, don't use cookies directly. Store things in the session.

Secondly, don't try and store model objects anyway. Store the ID, and retrieve the object via that ID when you need to:

request.session['journal_id'] = journal.pk

...

journal = Journal.objects.get(pk=request.session['journal_id'])
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!