TheRapture87
TheRapture87

Reputation: 1423

Django set_password no attribute

I'm trying to use the set_password() function but this error

'Member' object has no attribute 'set_password'

comes up when I use it. If I take out the set_password() function the password is stored in the database but without being hashed.

view.py

user = Member(username=u, password=p, email=e, security=s)
user.set_password(p)
user.save()

models.py

class Member(models.Model):
    username = models.CharField(max_length=16,primary_key=True)
    password = models.CharField(max_length=16)
    email = models.CharField(max_length=325)
    security = models.CharField(max_length=16)
    profile = models.OneToOneField(Profile, null=True)
    following = models.ManyToManyField("self", symmetrical=False)
    from_member_id = models.CharField(max_length=16)

    def __str__(self):
        return self.username

Upvotes: 0

Views: 5397

Answers (4)

Abbas Soloki
Abbas Soloki

Reputation: 117

Not sure if your doing this but it is probably easier to make the model with OneToOneField(User) and give it additional fields. You just need to remember to save in the new fields or the fields wont show up when u call. Where you set the user_form=Member(request.POST)

user = user_form.save()
user.set_password(user.password)
profile = user.userprofile
profile.bio = request.POST['bio']
profile.save()

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599926

The documentation on providing your own user model is quite clear and comprehensive. Among other things, your model must be a subclass of AbstractBaseUser, which is what provides the set_password method.

Also note that 16 characters is not nearly long enough to store a hashed, salted password.

Upvotes: 3

ilse2005
ilse2005

Reputation: 11439

As the error message tells you, the method set_password is not defined. Either you implement it yourself, or (better) create your Member model by subclassing django's AbrstactBaseUser:

class MyUser(AbstractBaseUser):
    username = models.CharField(max_length=16,primary_key=True)
    password = models.CharField(max_length=16)
    email = models.CharField(max_length=325)
    security = models.CharField(max_length=16)
    profile = models.OneToOneField(Profile, null=True)
    following = models.ManyToManyField("self", symmetrical=False)
    from_member_id = models.CharField(max_length=16)

    USERNAME_FIELD = 'username'

You can find more about custom user models in the django docs

Upvotes: 1

KaiBuxe
KaiBuxe

Reputation: 908

The set_password function is not automatically provided by models.Model. You have to define it by yourself or derive Member from django Usermodel

Upvotes: 6

Related Questions