What I'm trying to achieve is that I want to extend the profile model to a teacher or student. In the registration form, I added a selection field in which the user selects whether he is a teacher or student. Below is my model.
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 redefine the sign preservation 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 the teacher.save () or student.save () method is called, it causes an integrity error that is "(1048," Column "user_id" cannot be "null") ", but I am not creating a new user instance here. I I I'm trying to assign a newly created id profile to a teacher or student model. Am I doing wrong? What should I do?