title: To retrieve data from django database and display in table
body: <p>I am trying to retrieve data from db in django. I want to display it in a table.
  I am getting an error as:\</p>
  
  <p><strong>coercing to Unicode: need string or buffer, TransType found</strong> </p>
  
  <p>There are two models in my Models.py file:</p>
  
  <pre><code>class TransType(models.Model):
    name = models.TextField()
    created = models.DateTimeField(auto_now_add = True)
    updated = models.DateTimeField(auto_now = True) 
    def __unicode__(self):
        return self.name
  
  class Trans(models.Model):
    transtype = models.ForeignKey(TransType)
    script = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add = True)
    updated = models.DateTimeField(auto_now = True) 
    class Meta:
          unique_together = (("transtype", "script"),)
    def __unicode__(self):
        return self.transtype`
  </code></pre>
  
  <p>My views.py file</p>
  
  <pre><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.transtype_id = trans_type[0].id
                      if isinstance(pv[k], basestring):
                            script = pv[k]
                      else:
                            print "****** List ****"
                            script = pv[k][1]
                      trans.script = script
                      trans.save()
                      print " Inserted ==&gt;", script
                except Exception, e:
                      print e
                      #print "Not inserted ==&gt;", pv[k][1]
                      pass
    #return HttpResponse("Done")
    info = TransType.objects.all()
    info2 = Trans.objects.all()
    bookdata = { "details" : info, "details" : info2 }
    print bookdata
    return render_to_response("account/updatetrans.html", bookdata, context_instance=Context(request))
  </code></pre>
  
  <p>My url.py file</p>
  
  <pre><code>url(r'^updatetrans/$', 'booki.account.views.updatetrans', name='updatetrans'),
  </code></pre>
  
  <p>My updatetrans.html file</p>
  
  <pre><code>{% load i18n %}
  &lt;!doctype html&gt;
  &lt;html&gt;
  &lt;body&gt;
  
  &lt;button type="button" onclick="alert('Hello world!')"&gt;Click Me!&lt;/button&gt;
  &lt;table border="1" style="width:800px"&gt;
  &lt;tr&gt;&lt;td&gt;    
    {% for s in details %}
          {{ s.script }}
    {% endfor %} 
  &lt;/td&gt; 
  &lt;td&gt;     
    {% for n in detail %}
          {{ n.name }}
    {% endfor %} 
  &lt;/td&gt; 
  
  &lt;/tr&gt;
  
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Plz help....</p>
  
  <p><strong>Traceback</strong></p>
  
  <p>Environment:
  Request Method: GET</p>
  
  <pre><code>enter code here
  
  
  Django Version: 1.3
  Python Version: 2.7.3
  Installed Applications:
  ['django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.sites',
   'django.contrib.admin',
   'django.contrib.messages',
   'south',
   'booki.editor',
   'booki.account',
   'booki.reader',
   'booki.portal',
   'booki.messaging',
   'sputnik',
   'booktypecontrol']
   Installed Middleware:
   ('django.middleware.common.CommonMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.locale.LocaleMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.middleware.transaction.TransactionMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware')
  
   Traceback:
   File "/home/ttt/abc_booktype/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs)
  
   File "/home/ttt/abc_booktype/Booktype/lib/booki/account/views.py" in updatetrans 808.print bookdata
  
   File "/home/ttt/abc_booktype/local/lib/python2.7/site-packages/django/db/models/query.py" in __repr__72. return repr(data)
  
   File "/home/ttt/abc_booktype/local/lib/python2.7/site-packages/django/db/models/base.py" in __repr__370. u = unicode(self)
  
   Exception Type: TypeError at /accounts/updatetrans/
   Exception Value: coercing to Unicode: need string or buffer, TransType found
  </code></pre>