4

What i am trying to achieve is that i want to extend the profile model further to either teacher or student. In the signup form I added a choice field where user select whether he is teacher or student. Below is my model structure.

class Profile(UserenaLanguageBaseProfile):        
     """ Default profile """
     GENDER_CHOICES = (
             (1, _('Male')),
             (2, _('Female')),
         )

     user = models.OneToOneField(User,
                                unique=True,
                                verbose_name=_('user'),
                                related_name='profile')

    gender = models.PositiveSmallIntegerField(_('gender'),
                                              choices=GENDER_CHOICES,
                                              blank=True,
                                              null=True)

class Teacher(Profile):
    profile = models.OneToOneField(Profile,
                                unique=True,
                                verbose_name=_('profile'),
                                related_name='teacher')

    home_address =  models.CharField(_('home_address'), max_length=255, blank=True)
    home_phone =  models.CharField(_('home_phone'), max_length=30, blank=True)
    cell_phone =  models.CharField(_('cell_phone'), max_length=30, blank=True)
    experience =  models.IntegerField(default = 0)
    summary =  models.TextField(_('summary'), max_length=500, blank=True)

class Student(Profile):
    profile = models.OneToOneField(Profile,
                                unique=True,
                                verbose_name=_('profile'),
                                related_name='student')

    grade = models.CharField(_('grade'), max_length=50, blank=True)

I am overriding the signup save method as:

def save(self):
        new_user = super(SignupFormExtra, self).save()
        new_user.first_name = self.cleaned_data['first_name']
        new_user.last_name = self.cleaned_data['last_name']
        new_user.save()

        if self.cleaned_data['teacher_or_student'] == 'teacher':
            teacher = Teacher(profile = new_user.get_profile())
            teacher.save()
        elif self.cleaned_data['teacher_or_student'] == 'student':
            student = Student(profile = new_user.get_profile())
            student.save()
        return new_user

When teacher.save() or student.save() method is called it raises an integrity error that "(1048, "Column 'user_id' cannot be null")" but i am not creating a new user instance here i am trying to assign the newly created profile_id to teacher or student model. I am doing in the wrong way?? what should I do?

Leonardo
  • 4,046
  • 5
  • 44
  • 85
Aamir Rind
  • 38,793
  • 23
  • 126
  • 164

2 Answers2

3

As the error says you can't create a Student or Teacher without user as you've defined it as a non nullable field.

Make sure you're passing your class the new_user you've defined..

# ...
new_user.save()

if self.cleaned_data['teacher_or_student'] == 'teacher':
    teacher = Teacher(profile = new_user.get_profile(), user=new_user)
    teacher.save()
elif self.cleaned_data['teacher_or_student'] == 'student':
    student = Student(profile = new_user.get_profile(), user=new_user)
    student.save()
Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
2

I might be wrong, but why do you subclass your models from Profile model (so you have a "user" field in it already), and right after you have a "profile" OneToOneField field for Profile model again?

Leonardo
  • 4,046
  • 5
  • 44
  • 85
Andrey Shipilov
  • 1,986
  • 12
  • 14