2014-09-27 94 views
0

我在将我的django项目迁移到heroku时遇到了一些问题。在执行heroku运行python manage.py syncdb并输入超级用户和密码时,我收到以下错误消息。类型错误导致的Django错误:必须是unicode不是str

File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/text.py", line 409, in slugify 
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') 
TypeError: must be unicode, not str 

我的models.py

from django.db import models 
from django.db.models.signals import post_save 
from django.conf import settings 
from django.contrib.auth.models import User 
from autoslug import AutoSlugField 


# Create your models here. 
class Profile(models.Model): 
    account = models.OneToOneField(User, unique=True) 
    name = models.CharField(max_length = 120, null=True, blank=True) 
    location = models.CharField(max_length = 120, null=True, blank=True) 
    website = models.CharField(max_length = 120, null=True, blank=True) 
    bio = models.CharField(max_length = 120, null=True, blank=True) 
    timestamp = models.DateTimeField(auto_now = False, auto_now_add=True) 
    updated_timestamp = models.DateTimeField(auto_now = True, auto_now_add=False) 
    slug = AutoSlugField(populate_from="account") 

    @models.permalink 
    def get_absolute_url(self): 
     return ('view_profile', None, {'username': self.account.username}) 

    def __unicode__(self): 
     return str(self.account.username) 

# here is the profile model 
def user_post_save(sender, instance, created, **kwargs): 
    """Create a user profile when a new user account is created""" 
    if created == True: 
     p = Profile() 
     p.account = instance 
     p.save() 

post_save.connect(user_post_save, sender=User) 

回答

2

您试图在关系字段中使用AutoSlugField。您只能在返回文本的属性上使用AutoSlugField,而不能使用其他模型实例。创建一个返回一个属性,你slugify什么元素的用户:

class Profile(models.Model): 
    slug = AutoSlugField(populate_from="_accountname") 
    # [....] 

    @property 
    def _accountname(self): 
     return self.account.username 

在一个单独的说明:您__unicode__方法必须返回unicode对象,但你会返回一个str对象,而不是:

def __unicode__(self): 
    return str(self.account.username) 

删除str()电话:

def __unicode__(self): 
    return self.account.username 
2

尝试在您的个人资料模式转变unicode的功能如下:

def __unicode__(self): 
    return u'%s' % self.account.username 
+0

'self.account.username' is * a已经* unicode值。如果不是,你可以使用'unicode(self.account.username)'。 – 2014-09-27 15:28:20

+0

我试过了,它不起作用。我会尝试@MartijnPieters的答案 – cloudviz 2014-09-27 15:36:51

+0

@cloudviz:你没有包括你的整个回溯,所以我不得不做一些猜测;皮埃尔也做了,没有猜对。获得的经验:下一次包括整个回溯。 – 2014-09-27 15:37:39

相关问题