11

I want to serialize the values of a single model in Django. Because I want to use get(), values() is not available. However, I read on Google Groups that you can access the values with __dict__.

from django.http import HttpResponse, Http404
import json
from customer.models import Customer

def single(request, id):
    try:
        model = Customer.objects.get(id=id, user=1)
    except Customer.DoesNotExist:
        raise Http404
    values = model.__dict__
    print(values)
    string = json.dumps(values)
    return HttpResponse(string, content_type='application/json')

The print statement outputs this.

{'_state': <django.db.models.base.ModelState object at 0x0000000005556EF0>, 'web
site': 'http://example.com/', 'name': 'Company Name', 'id': 1, 'logo': '', 'use
r_id': 1, 'address3': 'City', 'notes': '', 'address2': 'Street 123', 'address1': 'Company Name', 'ustid': 'AB123456789', 'fullname': 'Full Name Of Company Inc.', 'mail': 'contact@example.com'}

Because of the _state key that holds an unserializable value the next line fails with this error.

<django.db.models.base.ModelState object at 0x0000000005556EF0> is not JSON serializable

How can I serialize the dictionary returned from __dict__ without _state being included?

danijar
  • 32,406
  • 45
  • 166
  • 297
  • possible duplicate of [django webapi: dump django model to JSON](http://stackoverflow.com/questions/16155277/django-webapi-dump-django-model-to-json) – Mp0int Apr 19 '14 at 12:45
  • @FallenAngel Serializing what `all()` returns is trivial, but my question is about `get()`. This is related but not a duplicate. – danijar Apr 19 '14 at 13:32

3 Answers3

17

model_to_dict() is what you need:

from django.forms.models import model_to_dict

data = model_to_dict(model)
data['logo'] = data['logo'].url
return HttpResponse(json.dumps(data), content_type='application/json')

By specifying fields and exclude keyword arguments you can control what fields to serialize.

Also, you can simplify the try/except block by using the shortcut get_object_or_404():

model = get_object_or_404(Customer, id=id, user=1)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks! I added the needed import to your question. However, I still can't serialize the dictionary since it contains a Django Image. I think the dictionary would have to contain the image url instead of the whole Django object. Any idea? – danijar Apr 19 '14 at 12:20
  • @danijar hm, how does it behave with the image field? Any errors? Also, could you try using built-in queryset serializer like suggested [here](http://stackoverflow.com/a/1124531/771848)? Thanks. – alecxe Apr 19 '14 at 12:27
  • `model_to_dict()` returns a dictionary like `{'name': 'Foo', 'user': 1, 'logo': }` which fails to serialize. The Django serializers wrap the fields dictionary in another one which I don't want. – danijar Apr 19 '14 at 12:34
  • @danijar ok, I've updated the answer. The idea is to use `model_to_dict()` to get the dictionary, then overwrite the `logo` key. Give it a try. There are other options, but let's first try this one. Thanks. – alecxe Apr 19 '14 at 12:40
  • I had to check for empty files so `values['logo'] = values['logo'].url if values['logo'] else ''` did the trick. Thanks for your help. – danijar Apr 19 '14 at 13:53
1

check the source code django/core/serializers/__init__.py comment:

Interfaces for serializing Django objects.

Usage::

    from django.core import serializers
    json = serializers.serialize("json", some_queryset)
    objects = list(serializers.deserialize("json", json))

To add your own serializers, use the SERIALIZATION_MODULES setting::

    SERIALIZATION_MODULES = {
        "csv" : "path.to.csv.serializer",
        "txt" : "path.to.txt.serializer",
    }

for one object

json = serializers.serialize("json", some_queryset[0:1])
vadimchin
  • 1,477
  • 1
  • 15
  • 17
0

I found out that is is actually possible to use values() together with get(). You just have to fetch values from a filtered set.

def single(request, id):
    user = 1
    try:
        models = Customer.objects.filter(id=id, user=user)
        values = models.values().get()
    except Customer.DoesNotExist:
        raise Http404
    string = json.dumps(values)
    return HttpResponse(string, content_type='application/json')
danijar
  • 32,406
  • 45
  • 166
  • 297