Python Django错误,TypeError:必须是unicode而不是str
在将django项目迁移到heroku时,我遇到了一些问题。在执行heroku运行python manage.py syncdb,然后键入超级用户和密码时,出现以下错误Python Django错误,TypeError:必须是unicode而不是str,python,django,signals,slug,Python,Django,Signals,Slug,在将django项目迁移到heroku时,我遇到了一些问题。在执行heroku运行python manage.py syncdb,然后键入超级用户和密码时,出现以下错误 File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/text.py", line 409, in slugify value = unicodedata.normalize('NFKD', value).encode('ascii', '
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
我的模特
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)
尝试将配置文件模型上的unicode函数更改为以下值:
def __unicode__(self):
return u'%s' % self.account.username
尝试将配置文件模型上的unicode函数更改为以下值:
def __unicode__(self):
return u'%s' % self.account.username
您正试图在关系字段上使用AutoSlugField。只能在返回文本的属性上自动lugfield,而不能在其他模型实例上自动lugfield。创建一个属性,该属性将返回要删除的用户元素:
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调用:
您正试图在关系字段上使用AutoSlugField。只能在返回文本的属性上自动lugfield,而不能在其他模型实例上自动lugfield。创建一个属性,该属性将返回要删除的用户元素:
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调用:
self.account.username已经是unicode值。如果不是,你可以用unicodeself.account.username。我试过了,没用。我将试着回答你的问题@MartijnPieters@cloudviz:你没有包括你的全部回溯,所以我不得不做一些猜测;皮埃尔也这样做了,但猜错了。经验教训:下次包括整个回溯。self.account.username已经是unicode值。如果不是,你可以用unicodeself.account.username。我试过了,没用。我将试着回答你的问题@MartijnPieters@cloudviz:你没有包括你的全部回溯,所以我不得不做一些猜测;皮埃尔也这样做了,但猜错了。经验教训:下次包括整个回溯。