title: How to insert data to django database without forms
body: <p>Hi I am trying to insert data to the database in django without forms.</p>
  <p>This is my views.py file</p>
  <pre class="lang-py prettyprint-override"><code>def updatetrans(request):
      json_data=open('/home/ttt/Abc/a.json').read()
      data = json.loads(json_data)
      for pk, pv in data.iteritems():
      for k,v in pv.iteritems():
          try:
               print k, &quot; =&gt;&gt; &quot;, pv['transcript'][1]
          except:
              pass
  </code></pre>
  <p>This is my url.py file</p>
  <pre class="lang-py prettyprint-override"><code>url(r'^updatetrans/$', 'booki.account.views.updatetrans', name='updatetrans'),
  </code></pre>
  <p>Here is my models.py file. I have created two tables. And want to insert data in both of them separately.</p>
  <pre class="lang-py prettyprint-override"><code>class TransType(models.Model):
      name = models.TextField()
      def __unicode__(self):
          return self.name
  
  class Trans(models.Model):
      trans = models.ForeignKey(TransType)
      script = models.CharField(max_length=200)
      def __unicode__(self):
          return self.trans
  </code></pre>
  <p>I am getting the output on console. That output I want to save to the database.
  Plz help.</p>
  <p>Thank you.. I tried some other way. I am getting error as:</p>
  <pre><code>global name 'TransType' is not defined
  Not inserted ==&gt; e
  </code></pre>
  <p>My code:</p>
  <pre class="lang-py prettyprint-override"><code>def updatetrans(request):
      json_data=open('/home/ttt/Ali/a.json').read()
      data = json.loads(json_data)
      for pk, pv in data.iteritems():
          for k,v in pv.iteritems():
              try:
                  trans_type = TransType.objects.get_or_create(name=k)
                  trans = Trans()
                  trans.trans_id = trans_type.id
                  trans.script = pv[k][1]
                  trans.save()
                  print &quot; Inserted ==&gt;&quot;, pv[k][1]
              except Exception, e:
                  print e
                  print &quot;Not inserted ==&gt;&quot;, pv[k][1]
                  pass
              return HttpResponse(&quot;Done&quot;)
  </code></pre>