如何使用不带用户表单的管理操作在django-registration中创建非活动用户帐户

时间:2013-07-09 15:09:30

标签: django django-registration

我是Python和Django的新手,正在尝试开发一个3步激活/注册系统。

第1步:使用电子邮件地址,名字和姓氏进行验证。

第2步:验证完成后,创建一个新的用户记录,其中包含要通过电子邮件发送的激活密钥和电子邮件地址作为记录用户名。

第3步:激活帐户。


我创建了一个负责第1步的应用程序,我已经能够结合django-registration和django自定义用户模型来处理第2步和第3步。

这一切都通过内置的管理工具在步骤1和2之间进行手动链接。但是,我希望有一个管理操作,可以从步骤1中选择所有已验证的用户,并使用这些记录自动处理第2步,而不必为每个用户使用表单。

我相信我应该能够调用RegistrationManager create_inactive_user方法来执行此操作。我的问题是,由于Python / Django知识有限,我很难调用这种方法。

实际上我认为我称之为正常,但是我遇到了第1步应用程序模型的错误。此模型使用BaseManager中的normalize_email实用程序,并在该应用程序中直接使用时工作,但是当我使用create_inactive_user时,我收到错误: / admin / simple_reg / emailreg /中的TypeError 'NoneType'对象不可调用

我认为有些东西我不包括在内。以下是我的admin.py文件中的相关详细信息:

from django.contrib import admin
from django.contrib.auth import get_user_model

from simple_reg.models import EmailReg       # Initial step 1 registration model
from userinfo.models import UserManager      # Step 2 and 3 custom user model
from registration.models import RegistrationManager


class EmailRegAdmin(admin.ModelAdmin):
...
    def create_user_record(self, request, queryset):
        # Generate new User records from validated email contacts
        count = 0
        validated = len(queryset)
        for contact in queryset:
            pword = 'randomletters'
            try:
                existing = get_user_model().objects.filter(email__exact=contact.email)
                if existing.exists():
                    print "A user with the email address of %s already exists." % (contact.email)
                else:
                    RegistrationManager().create_inactive_user(contact.first_name,
                                           contact.last_name, contact.email, pword, site)
                        count = count + 1
                except:
                    print "User %s was not created" % (contact.email)

        self.message_user(request, "Of %s validations, %s user accounts have been created." % (validated, count))

create_user_record.short_description = "Generate new User records on selected contacts"

我确信这很简单,所以希望有人能指出我正确的方向。

2 个答案:

答案 0 :(得分:1)

终于找到了答案。

我应该一直在调用RegistrationProfile,而不是RegistrationManager。

我知道这很简单!

答案 1 :(得分:0)

可能是最简单的方法,可以创建自己的用户模型并定义类似于布尔类型的is_active()的字段。