The model inherited from AbstractUser does not contain a hash password field

I have a model that is inherited from AbstractUser, something like this:

class Driver(AbstractUser): dni = models.CharField(max_length=8,validators=[validate_dni],unique=True) license = models.CharField(max_length=9,unique=True) birthday = models.DateField() sex = models.CharField(max_length=1, choices=SEX_CHOICES) creation_date = models.DateField(auto_now = True) 

According to this: https://docs.djangoproject.com/en/dev/topics/auth/customizing/

If you are completely satisfied with the Djangos User model and you just want to add additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your field profile. This class provides a complete implementation of the User as an abstract model.

But in my admin view, the password field is a simple text input, and the password is saved as the source text. I could try with AbstractBaseUser, but first I need to clarify this problem. I start with Django, so I'm a little newbie.

Thanks.

+4
source share
2 answers

You need to define a function to hash this password. I think you directly save it to the database.

 class MyForm(forms.ModelForm): ............ def save(self, commit=True): # Save the provided password in hashed format user = super(MyForm, self).save(commit=False) user.set_password(self.cleaned_data["password"]) if commit: user.save() return user 
+2
source

You do not need to define your own function. You just need to use the register with the UserAdmin class from django.contrib.auth.admin , and it works out of the box.

Obviously, in your admin.py file, make sure you have the following:

 from django.contrib.auth.admin import UserAdmin admin.site.register(CustomUserModel, UserAdmin) 
+3
source

Source: https://habr.com/ru/post/1468886/


All Articles