django-registration - 特定组的新用户

发布于 2024-11-04 09:14:25 字数 307 浏览 1 评论 0原文

我正在我的最新产品上使用(和学习)Django,我刚刚安装并开始使用 django-注册应用程序,允许我的用户自动注册。

一切正常,但我正在考虑将每个新用户分配到特定组(已创建),这样我就可以将管理员与普通用户“分开”。

我该怎么做? IMO,关于这个应用程序的文档很糟糕。

我没有创建任何模型或配置来开始使用 django-registration 应用程序。

I'm using (and learning) Django on my newest product, and I just installed and started using django-registration app to allow my users to auto-register themselves.

Everything's working good, but I'm thinking about assigning every new user to a specific group (already created), so I can "separate" the admins from the normal users.

How can I do this? The documentation about this app is awful, IMO.

I didn't create any model or configuration to start using the django-registration app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

成熟稳重的好男人 2024-11-11 09:14:25

我最终为此做了一个叉子,因为这让我发疯。

https://github.com/kcunning/django-registration

基本上,将以下内容添加到您的设置中.py 文件:

REGISTRATION_DEFAULT_GROUP_NAME="Groupname"

通过注册模块添加的任何用户将在创建时被赋予该组。

I ended up making a fork for this, as this was driving me crazy.

https://github.com/kcunning/django-registration

Basically, add the following to your settings.py file:

REGISTRATION_DEFAULT_GROUP_NAME="Groupname"

Any user added through the registration module will be given that group upon creation.

肩上的翅膀 2024-11-11 09:14:25

首先,我不是 dr (django-registration) 的专家,实际上只是检查了它,因为你在问题中提到了它。因此,如果其他人有更好的答案,请提交。无论如何,就这样吧。

我注意到创建用户(或 dr 称之为“配置文件”)的方式可以在位于registration.models.RegistrationManager 中的create_inactive_user 函数中找到。因此,在此函数中:

new_user.is_active = False
new_user.save()

您可以添加类似以下内容:

default_group = Group.objects.get(name=WHATEVER_THE_GROUP_NAME_IS)
user_group, is_created = UserGroup.get_or_create(user=new_user)
user_group.group = default_group
#user_group.groups.add(default_group)   # Django ver. 1.7+
user_group.save()

您可能可以在没有 get_or_create 的情况下执行此操作,因为我认为您无法使用相同的用户名创建多个用户,但我宁愿安全也不要抱歉。

此方法将导致您编辑 dr 文件(这可能不是最好的做法)并对某些内容(例如默认组名称)进行硬编码,但这应该是一个很好的起点。

附言。默认组名称可以是位于 settings.py 文件中的变量,这将使其更易于访问和美观。示例:

settings.py:
DEFAULT_GROUP_NAME = 'YOUR GROUP NAME'

registration/models.py:
default_group = Group.objects.get(name=settings.DEFAULT_GROUP_NAME)

无论如何,希望有所帮助。

To start, I'm not an expert at d-r (django-registration) and actually only checked it out because you mentioned it in your question. So, if anyone else has a better answer please submit it. Anyway, here it goes.

I noticed that the way users (or "profiles" as d-r calls them) are created is found in the create_inactive_user function located in registration.models.RegistrationManager. So, in this function after:

new_user.is_active = False
new_user.save()

You could add something like:

default_group = Group.objects.get(name=WHATEVER_THE_GROUP_NAME_IS)
user_group, is_created = UserGroup.get_or_create(user=new_user)
user_group.group = default_group
#user_group.groups.add(default_group)   # Django ver. 1.7+
user_group.save()

You could probably do without the get_or_create as I don't think you can create more than one user with the same username, but I would rather be safe than sorry.

This method will cause you to edit the d-r files (which may not be the best thing to do) along with hardcoding certain things (such as the default group name), but it should be a good stepping off point.

PS. the default group name could be a variable located in the settings.py file, which would make it more accessible and pretty. Example:

settings.py:
DEFAULT_GROUP_NAME = 'YOUR GROUP NAME'

registration/models.py:
default_group = Group.objects.get(name=settings.DEFAULT_GROUP_NAME)

Anyway, hope that helps.

冬天旳寂寞 2024-11-11 09:14:25

django-registration-redux 具有自定义信号,可用于将注册用户分配到特定组。 “user_registered”信号可用于此目的。请参阅 django-registration-redux 文档

from django.contrib.auth.models import Group


def user_created(sender, user, request, **kwargs):
    group = Group.objects.get(name='Kurum') 
    group.user_set.add(user)
    user.save()


from registration.signals import user_registered
user_registered.connect(user_created)

django-registration-redux has custom signals that can be used to assign registered user to a specific group. "user_registered" signal can be used for this purpose. Please refer to django-registration-redux documentation

from django.contrib.auth.models import Group


def user_created(sender, user, request, **kwargs):
    group = Group.objects.get(name='Kurum') 
    group.user_set.add(user)
    user.save()


from registration.signals import user_registered
user_registered.connect(user_created)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文