Django AttributeError: 'User' object has no attribute 'set_password' but user is not override

I have the following error:

AttributeError: 'User' object has no attribute 'set_password'

The problem is I didn't override the class User:

My model.py:

class User(models.Model):
    username = models.CharField(max_length=30)
    password = models.CharField(max_length=30)
    email = models.EmailField()

    def __str__(self):
        return self.username

My view.py:

def post(self, request):
    form = self.form_class(request.POST)

    if form.is_valid():

        user = form.save(commit=False)
        print type(user)
        # Cleaning and normalizing data
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user.set_password(password)
        user.save()

        # returns User objects if the credential are correct
        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('website:home')
    return render(request, self.template_name, {'form': form})

And this is my form.py:

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control',
                                                             'type': 'password',
                                                             'placeholder': 'Enter your password'}))
    class Meta:
        model = models.User

I don't really know also if I should override the User class. In which case I should and in which case I shouldn't?


You need to inherit from AbstractUser to get access to set_password attribute. Instead of using models.Model use:

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    ...

Your User model is not the same as django's User model.

Reference custom user model