Django注册和多个配置文件 [英] Django registration and multiple profiles

查看:181
本文介绍了Django注册和多个配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用django注册。我想用不同的配置文件创建不同类型的用户。
例如,一个用户是教师,另一个用户是学生。



如何修改注册以设置user_type并创建正确的配置文件? / p>

解决方案

长回答:p



我发现遗失手册对此类问题发表无价之宝,因为它解释了许多功能django-profile和django注册系统。



我建议使用多表继承



例如

 #models.py 
class Profile(models.Model):
#add任何常用字段(first_name,last_name和电子邮件来自用户)

#perhaps添加is_student或is_teacher在这里
@property
def is_student(self):
try:
self.student
返回True
除了Student.DoesNotExist:
返回False

类教师(个人资料):
#teacher fields

班学生(简介):
#student fields

django注册使用信号通知您的注册。您应该在此时创建配置文件,以便您确信对user.get_profile()的调用将始终返回配置文件。
使用的信号代码是

 #registration.signals.py 
user_registered = Signal(provide_args = [ user,request])

这意味着处理该信号时,您可以访问请求制作。所以当你发布注册表单时,包括一个字段来标识要创建的用户类型。

 #signals.py项目)
user_registered.connect(create_profile)

def create_profile(发件人,实例,请求,** kwargs):
从myapp.models导入配置文件,教师,学生

try:
user_type = request.POST ['usertype']。lower()
如果user_type ==teacher:#user .lower用于不区分大小写的比较
教师(用户=实例).save()
elif user_type ==学生:
学生(用户=实例).save()
其他:
配置文件(用户=实例).save()#Default create - 可能需要提高错误而不是
除了KeyError:
配置文件(user = instance).save()#Default只创建一个配置文件

如果您要为创建的模型添加任何内容,这些缺省字段值不在注册时间显然可以从请求中提取。


I'm using django-registration in my application. I want to create different kinds of users with different profiles. For example, one user is a teacher and another user is a student.

How can I modify registration to set the user_type and create the right profile?

解决方案

Long answer :p

I've found The Missing Manual post invaluable for this kind of problem as it explains many of features of the django-profiles and django-registration systems.

I'd suggest using multi table inheritance on the single profile you're allowed to set via the AUTH_PROFILE_MODULE

For instance

#models.py
class Profile(models.Model):
    #add any common fields here (first_name, last_name and email come from User)

    #perhaps add is_student or is_teacher properites here
    @property
    def is_student(self):
        try:
            self.student
            return True
        except Student.DoesNotExist:
            return False

class Teacher(Profile):
    #teacher fields

class Student(Profile):
    #student fields

django-registration uses signals to notify you of a registration. You should be creating the profile at that point so you are confident that calls to user.get_profile() will always return a profile. The signal code used is

#registration.signals.py
user_registered = Signal(providing_args=["user", "request"])

Which means when handling that signal you have access to the request made. So when you POST the registration form include a field that identifies what type of user to create.

#signals.py (in your project)
user_registered.connect(create_profile)

def create_profile(sender, instance, request, **kwargs):
    from myapp.models import Profile, Teacher, Student

    try:
        user_type = request.POST['usertype'].lower()
        if user_type == "teacher": #user .lower for case insensitive comparison
            Teacher(user = instance).save()
        elif user_type == "student":
            Student(user = instance).save()
        else:
            Profile(user = instance).save() #Default create - might want to raise error instead
    except KeyError:
        Profile(user = instance).save() #Default create just a profile

If you want to add anything to the model that is created, that isn't covered by default field values, at registration time you can obviously pull that from the request.

这篇关于Django注册和多个配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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