python - To retrieve data from django database and display in table -
i trying retrieve data db in django. want display in table. getting error as:\
coercing unicode: need string or buffer, transtype found
there 2 models in models.py file:
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`
my views.py file
def updatetrans(request): json_data=open('/home/ttt/ali/a.json').read() data = json.loads(json_data) pk, pv in data.iteritems(): 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 ==>", script except exception, e: print e #print "not inserted ==>", 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))
my url.py file
url(r'^updatetrans/$', 'booki.account.views.updatetrans', name='updatetrans'),
my updatetrans.html file
{% load i18n %} <!doctype html> <html> <body> <button type="button" onclick="alert('hello world!')">click me!</button> <table border="1" style="width:800px"> <tr><td> {% s in details %} {{ s.script }} {% endfor %} </td> <td> {% n in detail %} {{ n.name }} {% endfor %} </td> </tr> </table> </body> </html>
plz help....
traceback
environment: request method: get
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 @ /accounts/updatetrans/ exception value: coercing unicode: need string or buffer, transtype found
the traceback shows error coming in trans model's __unicode__
method. error says, need return unicode method, returning related transtype.
you fix explicitly converting unicode in method:
return unicode(self.transtype)
but should pick better string representation of data: there going many trans objects same transtype, unicode should distinguish between them, perhaps showing script field.
Comments
Post a Comment