I am working on a django project where I can't use django forms, and I'm facing a lot of problem when trying to access the abstract user objects,can anyone please tell me how to access these objects?
and here is my models.py file where I created a custom user so I can register and login using email.
models.py
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_("email address"), unique=True)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
class Shahadati_user(models.Model):
user = models.OneToOneField(CustomUser,on_delete=models.CASCADE)
user_nID=models.PositiveIntegerField(primary_key=True)
def __str__(self):
return self.user.email
below the views.py file, and the problem for me that I don't know how to access the first_name, last_name password, etc. objects that are in the abstract user model.
views.py
def user_reg(request):
registered = False
if request.method == "POST":
first_name = request.POST.get('FirstName')
last_name = request.POST.get('LastName')
nID = request.POST.get('nID')
Email = request.POST.get('em')
password = request.POST.get('psw')
######### the following code case the problem ########
if password == request.POST.get('pswValidation'):
hashed_password=make_password(password,None)
user = Shahadati_user (first_name=first_name,
last_name=last_name,
user_national_id=nID,
email=Email,
password=hashed_password,
)
user.save()
else:
raise ValidationError(_("Passwords are not the same !"))
return render(request, 'Register.html', {'regitered': registered})
source https://stackoverflow.com/questions/75761289/how-to-insert-data-into-user-model-without-using-django-forms
Comments
Post a Comment