Userena - 将Profile模型进一步扩展到两种不同的模型 [英] Userena - Extending the Profile model further upto two different models

查看:157
本文介绍了Userena - 将Profile模型进一步扩展到两种不同的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要实现的是我想将个人资料模型进一步扩展到教师或学生。在注册表单中,我添加了一个选择字段,用户选择他是否是教师或学生。以下是我的模型结构。

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

当调用teacher.save()或student.save()方法时,会引发完整性错误(1048,列user_id不能为空)但我没有在这里创建一个新的用户实例,我试图将新创建的profile_id分配给教师或学生模型。我在做错了吗?我应该怎么做?

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?

推荐答案

由于错误说你不能创建一个学生教师没有用户,因为您将其定义为不可空字段。

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

确保您传递了您定义的 new_user 的类。

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

这篇关于Userena - 将Profile模型进一步扩展到两种不同的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆