10

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

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

4

3 回答 3

12

长答案:p

我发现The Missing Manual帖子对于此类问题非常宝贵,因为它解释了 django-profiles 和 django-registration 系统的许多功能。

我建议在您可以通过 AUTH_PROFILE_MODULE 设置的单个配置文件上使用多表继承

例如

#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 使用信号通知您注册。您应该在此时创建配置文件,因此您确信对 user.get_profile() 的调用将始终返回配置文件。使用的信号代码是

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

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

#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

如果您想向创建的模型添加任何默认字段值未涵盖的内容,那么在注册时您显然可以从请求中提取该内容。

于 2011-04-07T12:50:32.440 回答
1

http://docs.djangoproject.com/en/1.2/topics/auth/#groups

Django 组是定义您要查找的内容的好方法。您可以拥有一份包含教师和学生所有属性​​的用户扩展配置文件。

class MasterProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    # add all the fields here

然后定义组:teacher并且student您将每个 MasterProfile 与教师或学生相关联。

Django Group 表可以帮助您定义各种角色并将用户正确分配到组。

于 2011-04-12T18:59:47.563 回答
1

我有同样的问题,我尝试了克里斯建议的答案,但它对我不起作用。

我只是 Django 的新手,但我认为处理程序采用的 argscreate_profile应该与providing_argsby signal下的那些相​​匹配,而在 Chris 的回答中他们不匹配(我认为它们可能与我见过user_registered的那些通过 signal 相匹配post_save他引用的缺失手册)

我修改了他的代码以使 args 匹配:

def create_profile(sender, **kwargs):
    """When user is registered also create a matching profile."""

    request, instance = kwargs['request'], kwargs['user']

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

现在似乎正在工作

于 2011-11-17T00:39:31.640 回答