[英]coercing to Unicode: need string or buffer, int found - Django Rest Framework
[英]Django: __unicode__ | coercing to Unicode: need string or buffer, OptionAttribute found
我有那些模型:
class Variation(models.Model):
barcode = models.CharField(max_length=255, unique=True, null=True, blank=True)
sku = models.CharField(max_length=255, unique=True, null=False, blank=False)
title = models.CharField(max_length=255, null=False, unique=False)
price = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
cost = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
stock_level = models.IntegerField(null=False, blank=False)
weight = models.DecimalField(null=True, blank=True, max_digits=5, decimal_places=5)
height = models.DecimalField(null=True, blank=True, max_digits=5, decimal_places=5)
width = models.DecimalField(null=True, blank=True, max_digits=5 decimal_places=5)
length = models.DecimalField(null=True, blank=True, max_digits=5, decimal_places=5)
is_master = models.BooleanField()
image_id = models.IntegerField(null=True, blank=True)
product = models.ForeignKey(Product, related_name='variations')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.title
class OptionType(models.Model):
name = models.CharField(max_length=255)
variation = models.ForeignKey(Variation, related_name='options')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.name
class OptionAttribute(models.Model):
value = models.CharField(max_length=255)
option_type = models.ForeignKey(OptionType)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.value
class Option(models.Model):
option_attribute = models.ForeignKey(OptionAttribute)
variation = models.ForeignKey(Variation)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.option_attribute
使用管理页面输入选项对象,这只是两个模型(外键)之间的关系,给我一个错误:
TypeError at /admin/backend/option/add/
coercing to Unicode: need string or buffer, OptionAttribute found
Request Method: POST
Request URL: http://192.168.100.10:5000/admin/backend/option/add/
Django Version: 1.8
Exception Type: TypeError
Exception Value:
coercing to Unicode: need string or buffer, OptionAttribute found
Exception Location: /home/vagrant/python/Platform/venv/local/lib/python2.7/site-packages/django/utils/encoding.py in force_text, line 92
Python Executable: /home/vagrant/python/Platform/venv/bin/python
Python Version: 2.7.3
Python Path:
['/home/vagrant/python/Platform/webshaper',
'/home/vagrant/python/Platform/venv/lib/python2.7',
'/home/vagrant/python/Platform/venv/lib/python2.7/plat-linux2',
'/home/vagrant/python/Platform/venv/lib/python2.7/lib-tk',
'/home/vagrant/python/Platform/venv/lib/python2.7/lib-old',
'/home/vagrant/python/Platform/venv/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/home/vagrant/python/Platform/venv/local/lib/python2.7/site-packages',
'/home/vagrant/python/Platform/venv/lib/python2.7/site-packages']
Server time: Fri, 31 Jul 2015 13:41:03 +0800
追溯: http : //dpaste.com/146YZP6
我用谷歌搜索了同样的问题,大多数问题是由unicode函数引起的,因为它无法返回Null值。 但是针对该问题建议的解决方案没有帮助。
任何人都不知道是什么原因造成的吗? 谢谢!
Unicode中的Foreign将是一个对象。 如果您想要ge foregin id。 返回str(self.option_attributes)可以
需要字符串或缓冲区,找到OptionAttribute
这告诉您,您的__unicode__()
方法返回的内容不是字符串或缓冲区(或unicode对象)。
从文档中 :
[
__unicode__()
...]应该返回一个Unicode对象
您返回一个OptionAttribute
对象,而python对此OptionAttribute
抱怨。
您可能要更换
def __unicode__(self):
return self.option_attribute
与
def __unicode__(self):
return unicode(self.option_attribute)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.