Django:__ unicode__ |强制转换为Unicode:需要字符串或缓冲区,找到OptionAttribute

时间:2015-07-31 09:34:54

标签: python django unicode typeerror

我有这些模特:

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

使用管理页面输入选项对象,它只是2个模型(外键)之间的关系给我一个错误:

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值。但针对该问题提出的解决方案并没有帮助。

任何人都知道造成这种情况的原因是什么? 谢谢!

2 个答案:

答案 0 :(得分:0)

unicode 中的Foreign将是一个对象。 如果你想ge是foregin id。 return str(self.option_attributes)就可以了

答案 1 :(得分:0)

  

需要字符串或缓冲区,找到OptionAttribute

这告诉您,您的__unicode__()方法返回的内容不是字符串或缓冲区(或unicode对象)。

来自the documentation

  

[__unicode__() ...]应该返回一个Unicode对象

你返回一个OptionAttribute对象,而python抱怨这个。

您可能想要替换

def __unicode__(self):
    return self.option_attribute

def __unicode__(self):
    return unicode(self.option_attribute)