1

I have to store some data in the window object to use it un the frontend rendering. I have a model:

from django.db import models
from tools.various.db import Base
from tools.files.fields import CustomImgField, IMAGES_DIRECTORY_ORIGINAL
from django.conf import settings

class myModel(Base):
    myName                     = models.CharField(max_length=100, verbose_name='myName')
    mySurname                  = models.CharField(max_length=100, verbose_name='mySurname')

I have a view:

from django.http import Http404
from django.views.generic import TemplateView
from django.http import JsonResponse
from json import dumps
from front.models import Language
from front.models import myModel


class BaseView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super(BaseView, self).get_context_data(**kwargs)

        context['myData'] = myModel.objects.value()
        return context

And I want to retrieve myData as a JSON object and store it in window object:

  window.app = {
    data: {},
    settings: {
      staticUrl: '{{ STATIC_URL }}',
      urls: {},
      storedData: {{ myData|jsonify|safe }}
    }
  };

But I get this response:

[{'myName': u'foo', 'mySurname': u'bar', u'id': 1L, 'order': 0L}] is not JSON serializable

Does anyone knows what I'm doing wrong?

Thanks!

EDIT:

I just tried return list(context) as André Laszlo proposes: it returns

Exception Value: list indices must be integers, not str

But if I use:

    context['myData'] = list(myModel.objects.values())
    return context

It seems to work. I'm going to read the documentation about 'list()'.

9
  • What does myModel.objects.value() return? Do you have an Order model? Commented Nov 11, 2017 at 19:07
  • Hi Blurp, with myModel.objects.value() I get the same result, [{'myName': u'foo', 'mySurname': u'bar', u'id': 1L, 'order': 0L}] is not JSON serializable Commented Nov 11, 2017 at 19:08
  • Can you show the value() method? Commented Nov 11, 2017 at 19:11
  • Possible duplicate of <Django object > is not JSON serializable Commented Nov 11, 2017 at 19:12
  • Sorry, it is values(), not value(): myModel.objects.values() Commented Nov 11, 2017 at 19:13

2 Answers 2

2

You are returning a python dictionary, you need to serialize it to JSON using json.dumps()

You are already importing json but not making use of it.

from django.http import Http404
from django.views.generic import TemplateView
from django.http import JsonResponse
from json import dumps
from front.models import Language
from front.models import myModel

class BaseView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super(BaseView, self).get_context_data(**kwargs)
        context['myData'] = myModel.objects.value()
        return dumps(context)

Additionally, you might want to read up on ujson - Faster than the builtin json library.

Preferably, if you are using Django 1.7+ you can do:

from django.http import Http404
from django.views.generic import TemplateView
from django.http import JsonResponse
from json import dumps
from front.models import Language
from front.models import myModel
from django.http import JsonResponse


class BaseView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super(BaseView, self).get_context_data(**kwargs)

        context['myData'] = myModel.objects.value()
        return JsonResponse(context)
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you alexis; with context['myData'] = list(myModel.objects.values()) it works; it is better to use json.dumps()?
Well, it's odd that it works by doing it that way. Try my approach, if it works that way, use it. Using JsonResponse from django.http is the best approach.
Also because by using that, it sets the correct response headers, content-type: application/json for you. You can read about it here: docs.djangoproject.com/en/1.11/ref/request-response/…
But, what if I have different contexts in my view, and I don't want to encode all of them? I imagine that if you apply return JsonResponse (context)you are encoding all of them.
You could then do return JsonResponse(context["ContextKey"])
|
2

I think that the problem is that your list is actually not a list, it's a QuerySet that just looks like a list when printed.

Have you tried:

context['myData'] = list(myModel.objects.values())
return context

I think it should return a list with dictionaries, which should be JSON serializeable. Please update your question with your results if it doesn't work.

2 Comments

Should be context['myData'] = list(myModel.objects.values())
Yes thank you, seems like OP got the idea at least :)