ranggatry
ranggatry

Reputation: 125

Error when update password in extending user models. Why and how to fix it ?

I have a function that user can update their email password. But, i use an extend model user to store email password in UserProfile Model.

This is my error

AttributeError at /simofa/ubah_password_email/59
'UserProfile' object has no attribute 'set_password'
Request Method: POST
Request URL:    http://localhost:8000/simofa/ubah_password_email/59
Django Version: 1.7.4
Exception Type: AttributeError
Exception Value:    
'UserProfile' object has no attribute 'set_password'
Exception Location: /home/boss/kantor/akun/simofa/views.py in ubah_password_email, line 277
Python Executable:  /usr/bin/python
Python Version: 2.7.6
Python Path:    
['/home/boss/kantor/akun',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-i386-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
Server time:    Thu, 12 Mar 2015 02:30:20 +0000 

This is my traceback:

Environment:


Request Method: POST
Request URL: http://localhost:8000/simofa/ubah_password_email/59

Django Version: 1.7.4
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'simofa',
 'accounts')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/boss/kantor/akun/simofa/views.py" in ubah_password_email
  277.                 email.set_password(user_profile.password_email)

Exception Type: AttributeError at /simofa/ubah_password_email/59
Exception Value: 'UserProfile' object has no attribute 'set_password'

This is my models

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

class UserProfile(models.Model):

user = models.OneToOneField(User) #digunakan untuk relasi ke model User (default) alias UserProfile adalah sebagai extending model
CATEGORY_CHOICES = (
    ('admin','Admin'),
    ('user','User'),
    )
hak_akses = models.CharField(max_length=100, choices = CATEGORY_CHOICES) 
password_email = models.CharField(max_length=100, blank=True)
password_pckelas = models.CharField(max_length=100, blank=True)

# Override the __unicode__() method to return out something meaningful!
def __unicode__(self):
    return self.user.username

This is my views

def ubah_password_email(request, pk):
    #cek session
    if 'username' in request.session and request.session['hak_akses'] == 'user':
        user = get_object_or_404(User, pk=pk) #ambil id dengan get
        profile = UserProfile.objects.filter(user=user).first() 
        email_form = EmailForm(data=request.POST, instance=profile) #gunakan instance untuk mengambil data yang sudah ada
        users = User.objects.all()
        if request.POST:
            if email_form.is_valid():
                email = email_form.save(commit=False)
                email.set_password(user_profile.password_email)
                email.save()
                return redirect('home')
        else:
            email_form = EmailForm(instance=profile)

        data = {
                'email_form': email_form,
                'object_list': users,
        }
        return render(request, 'ubah_password_email.html', data)
    else:
        return HttpResponseRedirect('/simofa/logout')
  1. How can i update my password_email in this case ?

I'm very grateful for your response. Thank you :)

Upvotes: 0

Views: 191

Answers (1)

vishen
vishen

Reputation: 469

So your EmailForm is returning a UserProfile model instance, and there is no function on UserProfile called set_password. I am guessing you actually want to do:

email.user.set_password(user_profile.password_email)

as the Django auth.User model does contain the set_password function.

Upvotes: 1

Related Questions