Python Userena-将概要文件模型进一步扩展到两种不同的模型

Python Userena-将概要文件模型进一步扩展到两种不同的模型,python,django,Python,Django,我试图实现的是,我想将概要模型进一步扩展到教师或学生。在注册表单中,我添加了一个选择字段,用户可以在其中选择自己是教师还是学生。下面是我的模型结构 class Profile(UserenaLanguageBaseProfile): """ Default profile """ GENDER_CHOICES = ( (1, _('Male')), (2, _('Female')), )

我试图实现的是,我想将概要模型进一步扩展到教师或学生。在注册表单中,我添加了一个选择字段,用户可以在其中选择自己是教师还是学生。下面是我的模型结构

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)
我正在将注册另存方法重写为:

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

当调用teacher.save()或student.save()方法时,它会引发一个完整性错误“(1048,“列'user\u id'不能为null”)。但我并不是在这里创建新的用户实例,而是试图将新创建的概要文件\u id分配给教师或学生模型。我做错事了??我该怎么办?

因为错误表明,如果没有
用户,则无法创建
学生
教师
,因为您已将其定义为不可为空的字段

确保您正在向类传递您定义的
new\u用户

# ...
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()

我可能错了,但为什么你要从概要模型中对模型进行子类化(这样你就已经有了一个“用户”字段),而在你又为概要模型创建了一个“概要”OneToOneField字段之后呢?

我太笨了。有时候这些小事会让你头疼。非常感谢你的朋友+1.