Python 自定义用户';使用表单进行数据挖掘和实现

Python 自定义用户';使用表单进行数据挖掘和实现,python,django,Python,Django,我在使用django注册redux时遇到问题 我想向用户添加其他字段,如生日、电话等,但我很难在模型中扩展用户类。向我的用户添加这些字段并在表单注册和登录中显示的最佳表单是什么?我建议按照以下步骤操作: 定义用户模型,添加所需字段,例如出生日期和照片: # filename: myapp/models.py from django.db import models from django.utils.translation import ugettext as _ from datetime

我在使用django注册redux时遇到问题


我想向用户添加其他字段,如生日、电话等,但我很难在模型中扩展用户类。向我的用户添加这些字段并在表单注册和登录中显示的最佳表单是什么?

我建议按照以下步骤操作:

  • 定义用户模型,添加所需字段,例如
    出生日期
    照片

    # filename: myapp/models.py
    
    from django.db import models
    from django.utils.translation import ugettext as _
    from datetime import datetime
    from django.conf import settings
    from django.contrib.auth.models import AbstractUser, Group
    
    class MyUser(AbstractUser):
    
        birth_date = models.DateField(null=True)
        photo = models.ImageField(upload_to=..., null=True, blank=True, verbose_name=_("photo"))
    
  • 创建自定义注册表:

    # in file myapp/forms.py
    from django import forms
    from registration.forms import RegistrationForm
    
    class MyRegistrationForm(RegistrationForm):
    
        birth_date = ...
        photo = ...
    
  • 编写自定义注册视图:

    # in file myapp/views.py
    
    from registration.backends.simple.views import RegistrationView
    from .forms import MyRegistrationForm
    
    class MyRegistrationView(RegistrationView):
    
        form_class = MyRegistrationForm
    
        def register(self, request, form):
    
            user = super(MyRegistrationView, self).register(request, form)
            user.birth_date = form.cleaned_data["birth_date"]
            user.photo = form.cleaned_data["photo"]
    
            user.save()
    
            return user
    
  • 告诉系统您将使用自定义用户模型

    # in file settings.py
    AUTH_USER_MODEL = "myapp.MyUser"
    
  • 添加url以调用自定义注册视图

    # in file urls.py
    
    from myapp.views import MyRegistrationView
    ...
    
    urlpatterns = [
        ...
        url(r'^accounts/register/$', MyRegistrationView.as_view(), name="registration_register"),
        ...
    ]
    

  • 我会考虑更改标题,因为它看起来与问题感谢的实际身体无关,我认为这个标题现在与正文更接近。我尝试了,但是在Mydial.Py中得到:错误:LuuGuel.MyUsR.Go:(field .e304)反向访问器,用于MySuulo.Cube与“用户组”的反向访问器冲突。提示:在“MyUser.groups”或“User.groups”的定义中添加或更改相关的\u name参数。Loguear.MyUser.user_权限:(fields.E304)“MyUser.user_权限”的反向访问器与“user.user_权限”的反向访问器冲突。系统检查发现4个问题(0个静音)。有什么问题吗?(也出现了其他类似错误)您似乎忘记了在
    settings.py
    中设置
    AUTH\u USER\u MODEL
    ,或者您在未加载的设置文件中这样做了。见:谢谢你的回复。我正在读这篇文章,我发现AUTH_USER_模型被弃用了。。。我的计划有什么意义?你能告诉我你在哪里读到的吗?这对我来说是新鲜事。顺便说一句,你成功了吗?我很抱歉。。。我对auth_profile_模块感到困惑,auth_USER_模型是可用的。。。但是它不起作用。当我尝试进行迁移时,它会说“您正在尝试在没有默认值的情况下向customuser添加一个不可为空的字段“password”。(customuser是我的型号)