Extended user model not save password correctly

im trying to create a child and parent objects at the same time with same form.

After migrations, i have two tables, user as always and doctor, whith the relationship with user in a extra column called user_ptr_id.

models.py:

class Doctor(User):
  collegiated = models.CharField(verbose_name="Nº Colegiado", max_length=20, null=True, blank=True)
  phone = models.CharField(verbose_name="Teléfono", max_length=12, null=True, blank=True)

class Patient(User):
  age = models.PositiveIntegerField(verbose_name="Edad", blank=True)

forms.py:

class DoctorForm(forms.ModelForm):

class Meta:
    model = app_models.Doctor
    fields = ['username', 'email', 'password']

view.py:

if request.method == 'POST:
  form = DoctorForm(request.POST.copy())
  if form.is_valid():
    forms.save()
else:
  form = DoctorForm()

return ...

The two objects are created, doctor and user, well related with user_ptr_id but the user password appears unencrypted. ¿ How can i integrate UserCreationForm on child models ? ¿ How can i solve this issue ?

Anybody could help me please? Thanks in advance.

Upvotes: 0

Views: 44

Answers (1)

Kishan Parmar
Kishan Parmar

Reputation: 820

use this in your views.py like this

also import make password

from django.contrib.auth.hashers import make_password

def user_signup(request):
    if request.method == "POST":
        user_form = userSignup(request.POST)
        phone = request.POST['phone']
        address = request.POST['address']
        pincode = request.POST['pincode']

        if user_form.is_valid() :
            # Hash password using make_password() function
            user = user_form.save(commit=False)
            user.password = make_password(user.password)
            user.save()

            ...

Upvotes: 1

Related Questions