title: How to pass model fields to a JsonResponse object
body: <p>Django 1.7 introduced the <a href="https://docs.djangoproject.com/en/1.7/ref/request-response/#jsonresponse-objects" rel="noreferrer">JsonResponse objects</a>, which I try to use to return a list of values to my ajax request.</p>
  
  <p>I want to pass</p>
  
  <pre><code>&gt;&gt;&gt; Genre.objects.values('name', 'color')
  [{'color': '8a3700', 'name': 'rock'}, {'color': 'ffff00', 'name': 'pop'}, {'color': '8f8f00', 'name': 'electronic'}, {'color': '9e009e', 'name': 'chillout'}, {'color': 'ff8838', 'name': 'indie'}, {'color': '0aff0a', 'name': 'techno'}, {'color': 'c20000', 'name': "drum'n'bass"}, {'color': '0000d6', 'name': 'worldmusic'}, {'color': 'a800a8', 'name': 'classic'}, {'color': 'dbdb00', 'name': 'hiphop'}]
  </code></pre>
  
  <p>to a JsonResponse object.</p>
  
  <p>However, my attempts fail.</p>
  
  <pre><code>&gt;&gt;&gt; JsonResponse({'foo': 'bar', 'blib': 'blab'}) # works
  &lt;django.http.response.JsonResponse object at 0x7f53d28bbb00&gt;
  
  &gt;&gt;&gt; JsonResponse(Genre.objects.values('name', 'color')) # doesn't work
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/home/marcel/Dokumente/django/FlushFM/env/lib/python3.4/site-packages/django/http/response.py", line 476, in __init__
      raise TypeError('In order to allow non-dict objects to be '
  TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False
  </code></pre>
  
  <p>This is probably due to the different data structure of <code>Genre.objects.values()</code>.</p>
  
  <p>How would this be done right?</p>
  
  <p>[edit]</p>
  
  <p>With <code>safe=False</code> I get</p>
  
  <pre><code>&gt;&gt;&gt; JsonResponse(Genre.objects.values('name', 'color'), safe=False)
  Traceback (most recent call last):
    File "&lt;console&gt;", line 1, in &lt;module&gt;
    File "/home/marcel/Dokumente/django/FlushFM/env/lib/python3.4/site-packages/django/http/response.py", line 479, in __init__
      data = json.dumps(data, cls=encoder)
    File "/usr/lib/python3.4/json/__init__.py", line 237, in dumps
      **kw).encode(obj)
    File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
      chunks = self.iterencode(o, _one_shot=True)
    File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
      return _iterencode(o, 0)
    File "/home/marcel/Dokumente/django/FlushFM/env/lib/python3.4/site-packages/django/core/serializers/json.py", line 109, in default
      return super(DjangoJSONEncoder, self).default(o)
    File "/usr/lib/python3.4/json/encoder.py", line 173, in default
      raise TypeError(repr(o) + " is not JSON serializable")
  TypeError: [{'color': '8a3700', 'name': 'rock'}, {'color': 'ffff00', 'name': 'pop'}, {'color': '8f8f00', 'name': 'electronic'}, {'color': '9e009e', 'name': 'chillout'}, {'color': 'ff8838', 'name': 'indie'}, {'color': '0aff0a', 'name': 'techno'}, {'color': 'c20000', 'name': "drum'n'bass"}, {'color': '0000d6', 'name': 'worldmusic'}, {'color': 'a800a8', 'name': 'classic'}, {'color': 'dbdb00', 'name': 'hiphop'}] is not JSON serializable
  </code></pre>
  
  <p>What works is</p>
  
  <pre><code>&gt;&gt;&gt; JsonResponse(list(Genre.objects.values('name', 'color')), safe=False)
  &lt;django.http.response.JsonResponse object at 0x7f53d28bb9e8&gt;
  </code></pre>
  
  <p>But isn't there a better way to generate a dict out of a Model object?</p>