TypeError at /UserRegis/ __init__() got an unexpec

2019-08-09 19:41发布

问题:

The question I meet is that, when I use UserReg.objects.create(), it will report things above. And I really had look up the solution for seaveral hours, but it doesn't work. So I want to know where I am wrong and how can I fixed it ?

PS:forgive me poor English,thanks a lot.

I'm sorry about it, these code just a part of all codes, and I just post some.

my total project is

  • project/
    • app1/...
    • app2/...
    • other_general(manage.py etc) ======I will post all files=======

app1/models.py

from django.db import models
from django.contrib.auth.models import  AbstractUser
from app2.models import SocialMed,Article


class UserReg(AbstractUser):

    nickname =  models.CharField(  max_length = 32 ,null = True, blank = True )
    avatar = models.ImageField( upload_to = '/media/client/avatar/%Y/%m/%d/' , null = True, blank = True)  # need to refer to something
    signature = models.CharField( max_length = 200, null = True , blank = True )
    user_uuid = models.IntegerField( null = True , blank = True,unique = True )
    user_focus = models.ManyToManyField( SocialMed ,null = True , blank = True )
    user_favor_article = models.ManyToManyField( Article, null = True, blank = True )


    def __unicode__(self):
        return self.nickname

    class Meta(AbstractUser.Meta):
         swappable = 'AUTH_USER_MODEL'
    
    def __init__(self,nickname):
          nickname = 'None'
          return nickname
         

app1/views.py

from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from client.models import UserReg
from app2.models import Article, SocialMed



def userRegiste( request ):

    if request.method == 'POST':
        user_name = request.POST.get('username')
        pass_word = request.POST.get('password')
        user_mail = request.POST.get('user_mail')
        clienter = UserReg.objects.create_user( username=user_name, password = pass_word,email = user_mail )
        print  clienter
        return render( request, 'register.html' )
    else:
        return render(request, 'register.html' )

TRACEBACK

Traceback Switch to copy-and-paste view

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
                    response = middleware_method(request, callback, callback_args, callback_kwargs)
                    if response:
                        break
            if response is None:
                wrapped_callback = self.make_view_atomic(callback)
                try:
                                response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
                except Exception as e:
                    # If the view raised an exception, run it through exception
                    # middleware, and if the exception middleware returns a
                    # response, use that. Otherwise, reraise the exception.
                    for middleware_method in self._exception_middleware:
                        response = middleware_method(request, e)
▶ Local vars
E:\django_file\zuiwan\client\views.py in userRegiste
def userRegiste( request ):
    if request.method == 'POST':
        user_name = request.POST.get('username')
        pass_word = request.POST.get('password')
        user_mail = request.POST.get('user_mail')
                    clienter = UserReg.objects.create_user( username=user_name, password = pass_word,email = user_mail ) ...
        print  clienter
        return render( request, 'register.html' )
    else:
        return render(request, 'register.html' )
▶ Local vars
C:\Python27\lib\site-packages\django\contrib\auth\models.py in create_user
                                             **extra_fields) ...
▶ Local vars
C:\Python27\lib\site-packages\django\contrib\auth\models.py in _create_user
                                      date_joined=now, **extra_fields) ...
▶ Local vars

that's all , and I realy need your any advice, please

回答1:

Replace

clienter = UserReg.objects.create_user( user_name, password = pass_word,email = user_mail )

with

clienter = UserReg.objects.create_user( username=user_name, password = pass_word,email = user_mail )


回答2:

You can try this in another way.

user = UserReg()
user.username = user_name
user.email = user_mail
user.set_password(pass_word)
user.save()

and try by adding this in your user model

    class Meta(AbstractUser.Meta):
         swappable = 'AUTH_USER_MODEL'