我正在尝试使用存储为会话数据的值来填充我的一个模型中的外键条目的值...这一切都运行良好但是当我尝试从管理员访问记录时我得到了这个错误:
Caught an exception while rendering: coercing to Unicode:
need string or buffer, Applicant found
其中Applicant
是由外键字段链接的模型。我该怎么解决这个问题?代码如下:
if "customer_details" in request.session:
customer = request.session["customer_details"]
else:
return HttpResponseRedirect('/application/')
if request.method == 'POST':
current_address_form = CurAddressForm(request.POST or None)
if current_address_form.is_valid():
current = current_address_form.save(commit=False)
current.customer = customer
current.save()
else:
current_address_form = CurAddressForm()
return render_to_response('customeraddress.html', {
'current_address_form': current_address_form,},
context_instance=RequestContext(request))
答案 0 :(得分:3)
看起来您正试图通过使用指向申请人的外键字段来输出模型的Unicode表示。
类似的东西(就在我头顶):
class Applicant(models.Model):
name = models.CharField(max_length=255)
class Foo(models.Model):
applicant = models.ForeignKey(Applicant)
def __unicode__(self):
# this won't work because __unicode__ must return an Unicode string!
return self.applicant
请向我们展示确定的型号代码。如果我的猜测是正确的,请确保__unicode__()
方法返回一个unicode对象。像这样:
def __unicode__(self):
return self.applicant.name
或
def __unicode__(self):
return unicode(self.applicant)