Django 注册和多个配置文件
我在我的应用程序中使用 django-registration 。我想创建具有不同配置文件的不同类型的用户。 例如,一个用户是教师,另一个用户是学生。
如何修改注册以设置 user_type 并创建正确的配置文件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
长答案:p
我发现缺失的手册帖子对于此类问题解释了 django-profiles 和 django-registration 系统的许多功能。
我建议使用 多表继承在单个配置文件上,您可以通过 AUTH_PROFILE_MODULE 设置,
例如
django-registration 使用信号来通知您注册。您应该在此时创建配置文件,以便您确信对 user.get_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
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
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.
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.
http://docs.djangoproject.com/en/1.2/topics/auth/ #groups
Django 组是定义您要查找的内容的好方法。
您可以拥有一个用户扩展配置文件 将包含教师和学生的所有属性。
然后定义组:
教师
和学生
,并将每个 MasterProfile 与教师或学生关联。Django 组表可以帮助您定义各种角色并将用户正确分配到组。
http://docs.djangoproject.com/en/1.2/topics/auth/#groups
Django groups are a great way to define what you are looking for.
You can have one User extended profile that will contain all attributes of teachers and students.
Then define groups:
teacher
andstudent
and you associate each MasterProfile to either a teacher or a student.Django Group table can help you define your various roles and allocate users to groups correctly.
我遇到了同样的问题,我尝试了克里斯建议的答案,但它对我不起作用。
我只是 Django 的新手,但我认为处理程序
create_profile
所采用的参数应该与信号user_registered
下的providing_args
下的参数相匹配,并且在克里斯的回答他们不(我认为它们可能与信号post_save
传递的信号相匹配,我在他引用的《缺失手册》中看到了该信号的使用)我修改了他的代码以使 args 匹配:
并且似乎现在正在工作
I had the same issue and I tried the answer suggested by Chris, however it didn't work for me.
I'm only a newbie in Django, but I think the args taken by handler
create_profile
should match those underproviding_args
by signaluser_registered
, and in Chris's answer they don't (I think they probably match those passed by signalpost_save
, which I've seen used in the Missing Manual that he quotes)I modified his code to make args match:
and seems to be working now