简体   繁体   中英

How to create a user with AbstractBaseUser or AbstractUser. (Login Error)

I am trying to use my Person class as user in models.py. However I tried using AbstractUser and AbstractBaseUser. Both of them give me errors in the part where I try to log in, I tried several ways until I realized that I need help from someone with more experience and knowledge than I do.

My models.py:

from django.contrib.auth.models import AbstractUser
from django.db import models

class Pessoa(AbstractUser):
    id_pessoa = models.AutoField(primary_key=True)
    cnpj = models.CharField(max_length=14, unique=True)
    nome = models.CharField(max_length=70)
    email = models.CharField(max_length=100, blank=True, null=True)
    senha = models.CharField(max_length=40, null=False)
    ativo = models.BooleanField(blank=True, null=True)
    datacadastro = models.DateField(blank=True, null=True)
    cidade = models.CharField(max_length=50, blank=True, null=True)
    uf = models.CharField(max_length=2, blank=True, null=True)
    USERNAME_FIELD = 'cnpj'

    class Meta:
        managed = False
        db_table = 'pessoa'

Detail: I am unable to remove or add fields in any template I am using, because I have used inespectdb to create the models based on the database i am using ( the same one that is already full of data ). Any change, with the exception of basic changes , is out of the cogitation.

Below my views.py with def that validates the login and redirects to the dashboard .

def dash_login(request):
    form = LoginForm(request.POST, None)
    if request.method == 'POST':
        #if form.is_valid():
        username = form.data['cnpj']
        password = form.data['senha']
        user = authenticate(username=username)
        print(username)
        #p = Pessoa.objects.filter(cnpj=form.data['cnpj']).values('id_pessoa')[0]['id_pessoa']
        #login(request, user)
        print(request.user.is_authenticated)
        #return render(request, 'dashboard.html')
    return render(request, 'login.html', {'form': form})

I use print() to test the commands before running out, at the moment when it arrives on the line:

user = authenticate(username=username)

It triggers this error:

File"C:\\Users\\Pichau\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\django\\db\\backends\\utils.py", line 85, in _execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: column pessoa.password does not exist LINE 1: SELECT "pessoa"."password", "pessoa"."last_login", "pessoa"....

** Regardless I put password inside the authenticate error is shot. **

This was the second option:

Where I fall into the same error, if I remove the p and use return render (request, 'dashboard.html') I'm redirecting without the user id, which causes me problems getting its data inside the dashboard.

However I still have the main problem, this table is logging in to Django login and not in the login I created within models.py | forms.py models.py | forms.py

Anyway, appreciate the help for anything in this post, will be grateful for anyone who give me any direction.

There's no ability to set an option for the password field the way you can with USERNAME_FIELD. However, one thing to bear in mind is that the name of the Django model field doesn't have to match what's in the underlying db table; you can use the db_column option on your field definition:

password = models.CharField(max_length=40, null=False, db_column='senha')

The other issue is that you should certainly be using AbstractBaseUser, not AbstractUser; the latter adds a load of fields that you don't want or have.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM