13

I have a model that is inherited of 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’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model.

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

Thanks.

0

3 Answers 3

21

You don't have to actually define your own function. You just need to use register it with the UserAdmin class from django.contrib.auth.admin and it works out of the box.

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

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

If you have additional custom fields on your model, the above way of registering will make them not shown in the admin. In this case, you can make it work by making your custom Admin class inherit from the UserAdmin class, like the following:

from django.contrib import admin from django.contrib.auth.admin import UserAdmin @admin.register(CustomUserModel) class CustomUserModelAdmin(UserAdmin): ... 
Sign up to request clarification or add additional context in comments.

6 Comments

When I register it with UserAdmin my custom options disappear. Any idea why is that?
Yeah, the admin classes specify what fields and such appear. You can make a new class that inherits from UserAdmin and includes your custom fields. Then register that new admin class with your custom user model.
my custom fields disappear from the admin after adding this line admin.site.register(CustomUserModel, UserAdmin)
I have the same thing
@mr_bulrathi: I updated the answer to include an example with a custom admin model which is what you'll need to do to get both the password hashing correct as well as have your custom fields shown.
|
6

You need to define a function to hash that password. I think you directly save it to 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 

1 Comment

Thanks, it works. I thought that it isn't necessary to edit the save method or call to "set_password" method explicitly.
-1

Procedure djnago >=3.0

models.py

from django.db import models from django.contrib.auth.models import AbstractUser YEAR_IN_SCHOOL_CHOICES = [('1', '1 class'),('2', '2 class'),('3', '3 class'),('4', '4 class'),('5', '5 class'),('6', '6 class'),('7', '7 class'),('8', '8 class'),('9', '9 class'),('10', '10th class'),] class User(AbstractUser): type = models.CharField(max_length=256, choices=(('1','Student'), ('2','Professor'), ('3','lower_staf')), default='1') class Student(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True ) gender = models.BooleanField(choices=((1,'Male'), (2,'Female'), (3,'Trans')), def __str__(self): return '{}'.format(self.user) 

admin.py

from django.contrib import admin from.models import User, Student from django.contrib.auth.admin import UserAdmin class CustomUserAdmin(UserAdmin): fieldsets = UserAdmin.fieldsets + ((None, {'fields': ('type',)}),) add_fieldsets = UserAdmin.add_fieldsets + ((None, {'fields': ('type',)}),) class Student_admin(admin.ModelAdmin): pass admin.site.register(User, CustomUserAdmin) admin.site.register(Student, Student_admin) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.