得到了一个意外的关键字参数'user'

时间:2017-04-27 21:58:00

标签: python django django-models

我试图理解为什么我的代码不能正常工作。

这是模特:

class Usuario(models.Model):
    user = models.OneToOneField(User,default=None)
    nombre = models.CharField(max_length=100)
    correo = models.CharField(max_length=300)
    tel = models.CharField(max_length=100)
    cp = models.CharField(max_length=100)
    def __unicode__(self):
        return self.nombre

这是观点:

def RegistrosUsuarios(request):
    if request.method == "POST":
        nombre = request.POST['nombre']
        correo = request.POST['correo']
        contrasena = request.POST['contrasena']
        mailused = None
        try:
           mailused = User.objects.get(email=correo)
        except User.DoesNotExist:
            print("Usuario no existe")
        if mailused is None:
            user = User.objects.create_user(username=correo, email=correo)
            user.set_password(contrasena)
            user.save()
            ultimoUser = User.objects.all().last()
            usuario = Usuario(user=ultimoUser,nombre=nombre,correo=correo)
            usuario.save()
            user = authenticate(username=user.username,password=contrasena)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect('/')
                else:
                    return HttpResponse("inactive user")
            else:
                return HttpResponseRedirect('/')
        else:
            return HttpResponseRedirect('/')    
    else:
        return HttpResponseRedirect('/')

表格:

<form  action="/RegistrosUsuarios/" method="post" enctype="multipart/form-data" autocomplete="off">
                {% csrf_token %}
                <div class="modal-body">         
                    <div class="md-form">
                        <input maxlength="500" name="nombre" type="text" placeholder="Nombre" required="required" style="width: 25%;">
                        <label for="nombre" style="color: #000!important;display: none;" class="active"></label>
                    </div> 
                    <div class="md-form">
                        <input type="email" name="correo" id="correo" required="required" placeholder="Correo" style="width: 25%;">
                        <label for="correo" style="color: #000!important;display: none;" class="active">Email</label>
                    </div>
                    <div class="md-form">
                        <input type="password" name="contrasena" id="contrasena" required="required" placeholder="Contraseña" style="width: 25%;">
                        <label for="contrasena" style="color: #000!important;display: none;" class="active">Contraseña</label>
                    </div>
                    <div class="input-field" style="border-top:0px ;align:center;" align="center">
                        <button type="submit" name="action" class="btn waves-effect waves-light" style="border: 2px solid #000;color: #000!important; width: 15%;background-color:transparent!important;font-family: Geomanist-Regular;letter-spacing: 0px;font-size: 1.1rem; ">REGISTRARSE</button>
                    </div>
                </div>  
            </form>

网址:

urlpatterns = [
    url(r'^$', views.Index),
    ...
    url(r'^RegistrosUsuarios/$', views.RegistrosUsuarios),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

但是,当我尝试从Usuario模型创建一个对象时:

TypeError at /RegistrosUsuarios/
Usuario() got an unexpected keyword argument 'user'
Request Method: POST
Request URL:    http://127.0.0.1:8000/RegistrosUsuarios/
Django Version: 1.11
Exception Type: TypeError
Exception Value:    
Usuario() got an unexpected keyword argument 'user'

1 个答案:

答案 0 :(得分:1)

看起来你有一个名为Usuario的方法,它与Usuario模型类一起导入。

您还可以使用默认管理器的.create()方法创建对象,而不是手动实例化并保存它们:

Usuario.objects.create(user=..., ...)

这样您就不需要调用.save()方法,因为它会自动保存。

作为旁注,建议您遵循PEP 8命名约定,并在lowercase_letters中为您的方法命名,并为您的班级使用CamelCase。这将减少此类错误的数量。