1
I’m trying to use my class Pessoa
as a user in models.py
.
Meanwhile I tried to use AbstractUser
and AbstractBaseUser
. Both return 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 that I.
Man py.models:
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 model I’m using because I used
inespectdb
for create the models based on the database I’m using(The even though it’s already full of data). Any change, with exceptions of basic change, is out of the question.
Below my views.py
with the def
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 exiting by running, at the moment when it arrives at the line:
user = authenticate(username=username)
He makes this mistake:
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 "person"." password", "person"." last_login", "person"....
Regardless of whether I put password inside the
authenticate
the mistake is shot.
That was the second option:
def dash_login(request):
form = LoginForm(request.POST, None)
if request.method == 'POST':
print(form.is_valid())
if form.is_valid():
cnpj = form.data['cnpj']
print('Valido')
print(cnpj)
p = Pessoa.objects.filter(cnpj=cnpj).values('id_pessoa')[0]['id_pessoa']
#print(p)
#return redirect('dashboard', p)
return render(request, 'login.html', {'form': form})
Where I fall into the same mistake, in case I withdraw the p
and use return render(request, 'dashboard.html')
I am redirecting without user id, which causes me problems to get data from it inside Dashboard.
However I still have the main problem, this table is logging in the Django login and not the login I created inside the models.py
|forms.py
Edit1: models.py I’m trying to integrate.
from django.contrib.auth.models import AbstractUser, User
from django.db import models
from django.conf import settings
class Pessoa(AbstractUser):
id_pessoa = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, 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'
REQUIRED_FIELDS = []
class Meta:
managed = False
db_table = 'pessoa'
def __str__(self):
return self.nome
Error message when giving migrate:
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: syntax error at or near "WITH ORDINALITY"
LINE 6: FROM unnest(c.conkey) WITH ORDINALITY co...
Complete error https://i.stack.Imgur.com/vV54h.png
The fact that I can’t give migrate
explains the previous error that explains that the component does not exist in the table
of model
.
Edit: The error was generated when running
inspectdb
and the bank was with an outdated version(Postgresql), it was necessary to update for a newer version and run the code again, to be able inspect your bank and create templates from it.Another possible mistake is that it prevents you from giving
migrate
for that same error, and this can cause field fouling errors in the database by cannot givemigrate
After creating the new model
Pessoa
you spun thepython manage.py makemigrations
and then thepython manage.py migrate
?– mazulo
Yes, I’ve even created a test backup, and I’ve tested several coiass, I don’t know where I’m going wrong. Even things work, IF, I don’t try to pull some information from Person.Bjects
– Nicolas
I would guess then that you would not have added your app Django where the model
Pessoa
is set there in theINSTALLED_APPS
ofsettings.py
. This error that appears is exactly why Django is trying to use a table in the database that does not yet exist.– mazulo
@It’s already there. Actually I’m having an error in giving migrate, which prevents me from creating the table via Django so that the field can exist.
– Nicolas
Put this information in the question too, along with the complete error message.
– mazulo