2
我正在尝试设置Django来为作者提供配置文件。它需要一个非常简单的解决方案,我有什么,我相信应该工作:Django Profiles
型号
from django.contrib.auth.models import User
class MyProfile(models.Model):
"Extends the user object for custom attr"
user = models.OneToOneField(User)
website = models.URLField(blank=True,null=True)
websiteName = models.CharField(max_length=255,blank=True,null=True)
twitterHandle=models.CharField(max_length=255,blank=True,null=True)
bio = models.TextField()
User.profile = property(lambda u: MyProfile.objects.get_or_create(user=u)[0])
def __unicode__(self):
return self.user
设置
AUTH_PROFILE_MODULE = "btc_app.UserProfile"
然而,每当我创建一个配置文件,我得到一个错误类型:
Exception Value: coercing to Unicode: need string or buffer, User found
我的发布信息看起来像这样
POST
website u''
bio u'tttt'
_save u'Save'
twitterHandle u''
user u'2'
websiteName u''
csrfmiddlewaretoken u'5af9526bc84673fc3338d63272804b92'
有一个下拉的所有用户,它可以让我选择一个,但由于某种原因,保存失败......任何想法?
感谢, CG
注意:您不需要'User.profile = ...'代码。您可以通过使用Django内置方法User.get_profile()来访问用户的配置文件,该方法将为用户返回已注册的配置文件模型。参考:https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.get_profile –