Django:ManyToManyField未定义错误

时间:2014-06-08 15:56:34

标签: python django django-userena

我一直在尝试在django-userena中编辑用户个人资料,除了我的ManyToManyFields之外一切都很好。我设置了以下模型:

class MyProfile(UserenaBaseProfile):
    user = models.OneToOneField(User,unique=True,  
                        verbose_name=_('user'),related_name='my_profile')
    favourite_snack = models.CharField(_('favourite snack'),max_length=5) 
    firstname = models.CharField(_('username'), max_length=55)
    lastname = models.CharField(_('lastname'), max_length=66)
    industry = models.CharField(_('industry'), max_length=190)
    occupation = models.CharField(_('occupation'), max_length=140)
    bio = models.TextField(_('bio'))
    phone = models.CharField(_('phone'), max_length=10)
    skills = models.ManyToManyField(Skills)
    Interests = models.ManyToManyField(Interests)
    website = models.URLField()

class Skills(models.Model):
    skillName = models.CharField(max_length=80)

class Interests(models.Model):
    interestName = models.CharField(max_length=140)

当我同步数据库时,我收到以下错误:

 File "/pathtodjango/bin/app/accounts/models.py", line 17, in MyProfile
    skills = models.ManyToManyField(Skills)
NameError: name 'Skills' is not defined

2 个答案:

答案 0 :(得分:14)

使用延迟参考:

skills = models.ManyToManyField('Skills')

答案 1 :(得分:4)

问题是您在定义模型类Skills之前正在考虑它。 请注意,Python不像Javascript那样提升功能(类)。 所以你的代码应该是这样的

class Skills(models.Model):
    skillName = models.CharField(max_length=80)

class Interests(models.Model):
    interestName = models.CharField(max_length=140)


class MyProfile(UserenaBaseProfile):
    user = models.OneToOneField(User,unique=True,  
                        verbose_name=_('user'),related_name='my_profile')
    favourite_snack = models.CharField(_('favourite snack'),max_length=5) 
    firstname = models.CharField(_('username'), max_length=55)
    lastname = models.CharField(_('lastname'), max_length=66)
    industry = models.CharField(_('industry'), max_length=190)
    occupation = models.CharField(_('occupation'), max_length=140)
    bio = models.TextField(_('bio'))
    phone = models.CharField(_('phone'), max_length=10)
    skills = models.ManyToManyField(Skills)
    Interests = models.ManyToManyField(Interests)
    website = models.URLField()