通过表单调用自定义create_user失败

时间:2017-02-27 18:01:51

标签: python django

我有一个表单,我将用于用户注册,并希望使用我的自定义用户管理器中定义的相同create_user函数,但是,当提交用户数据时,值将进入数据库以与输入内容不对应的方式,而不是每次,用户email_address被输入为“abstractuser.MyUser.object”而不是给予表单的内容。如何修改此代码以将正确的值传递给create_user函数并成功创建用户? 感谢。

Models.py

from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin

# Create your models here.
class MyManager(BaseUserManager):
    def create_superuser(self, email_address, password):
        superuser = MyUser()
        superuser.email_address = email_address
        superuser.set_password(password)
        superuser.is_staff = True
        superuser.is_superuser = True
        superuser.save()

    def create_user(self, email_address, password):
        user = MyUser()
        user.email_address = email_address
        user.set_password(password)
        user.is_staff = True
        user.is_superuser = False
        user.save()
        return user

class MyUser(AbstractBaseUser, PermissionsMixin):
    email_address = models.EmailField(unique=True, max_length=100)
    is_staff = models.BooleanField(default=0)

    object = MyManager()

    USERNAME_FIELD = 'email_address'

    def get_short_name(self):
        return self.email_address

Forms.py

from django.forms import ModelForm
from django import forms
from models import MyUser

class AuthForm(ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    class Meta:
        model = MyUser
        fields = ['email_address', 'password']

Views.py

from django.shortcuts import render
from forms import AuthForm
from django.contrib.auth import get_user_model

# Create your views here.
def user_create(request):
    if request.method == "POST":
        get_user_model().object.create_user(self, request.POST)

    return render(request, 'abstractuser/user_create.html', {'form': AuthForm})

修改

感谢大家的输入,我已将self参数添加到create_user函数并将self参数添加到对create_user的调用中,但是,现在我收到错误,'全局名称'self'未定义”。在这种情况下获得自我的正确方法是什么?

TRACEBACK

Environment:


Request Method: POST
Request URL: http://localhost:8000/user/create

Django Version: 1.10.5
Python Version: 2.7.12
Installed Applications:
['abstractuser',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/dev/.local/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/home/dev/.local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/dev/.local/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/dev/DjangoProjects/abstracttest/abstractuser/views.py" in user_create
  8.         get_user_model().object.create_user(self, request.POST)

Exception Type: NameError at /user/create
Exception Value: global name 'self' is not defined

2 个答案:

答案 0 :(得分:1)

你可以复制我的代码。乍一看没什么区别。

models.py

Find

然后您可以从具有用户实例的视图中调用 create_user。

答案 1 :(得分:0)

感谢Sanca的输入,但是,已通过views.py user_create函数的以下添加解决了这个问题

get_user_model().object.create_user(request.POST['email_address'],request.POST['password'])

和models.py MyManager.create_user如下所示:

def create_user(self, email_address, password):
    user = MyUser()
    user.email_address = email_address
    user.set_password(password)
    user.is_staff = True
    user.is_superuser = False
    user.save()
    return user