Django JSONField inside ArrayField
<p>I have a problem inserting to a field using ArrayField with JSONField inside.</p>
<p><strong>models.py</strong></p>
<pre><code>locations = ArrayField(JSONField(null = True,blank = True), blank=True, null = True)
</code></pre>
<p><strong>Insert</strong></p>
<pre><code>location_arr = [{&quot;locations&quot; : &quot;loc1&quot;,&quot;amount&quot;:Decimal(100.00)},{&quot;locations&quot; : &quot;loc2&quot;,&quot;amount&quot;:Decimal(200.25)}]
instance.locations = location_arr
instance.save()
</code></pre>
<p>When I do this, I got</p>
<blockquote>
<p>column &quot;locations&quot; is of type jsonb[] but expression is of type text[]</p>
<p>LINE 1: ...d&quot; = 2517, &quot;locations&quot; = ARRAY['{&quot;loc...</p>
<p>Hint: You will need to rewrite or cast the expression.</p>
</blockquote>
<p>So I tried to dump it using:</p>
<pre><code>import json
location_arr = [{&quot;locations&quot; : &quot;loc1&quot;,&quot;amount&quot;:Decimal(100.00)},{&quot;locations&quot; : &quot;loc2&quot;,&quot;amount&quot;:Decimal(200.25)}]
instance.locations = json.dumps(location_arr)
instance.save()
</code></pre>
<p>then I got this</p>
<blockquote>
<p>LINE 1: ...d&quot; = 2517, &quot;locations&quot; = '[{&quot;loc&quot;:...</p>
<p>DETAIL:  &quot;[&quot; must introduce explicitly-specified array dimensions.</p>
</blockquote>
<p>I am using:</p>
<ol>
<li>Django 1.9</li>
<li>Python 2.7</li>
<li>Postgres 9.4.10</li>
<li>psycopg2 2.6.2</li>
</ol>