1

I have been trying to edit the user profile in django-userena and everything is fine except my ManyToManyFields. I have the following models set up:

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)

And when I sync the database I get the following errors:

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

2 Answers2

20

Use lazy reference:

skills = models.ManyToManyField('Skills')
likeon
  • 791
  • 1
  • 6
  • 19
4

The issue is that you are reffering the model class Skills before defining it. Beware that Python doesn't Function (class) hoisting as Javascript does. So your code should be like this

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()
rogue-one
  • 11,259
  • 7
  • 53
  • 75
  • 2
    Keep in mind that this solution only works if you don't have a circular reference, otherwise the only possible solution would be @likeon 's answer – Alejandro Garcia Jun 08 '14 at 16:30