create-user-model.mo
Last updated
Last updated
You have a Django project installed.
You need to handle different types of users (for instance an Actor, a Producer, a Director) that will all inherit from the user, but will have different properties.
You want to handle them from the admin.
Have the project installed,
You have a basic user model with a name.
You want to create another type of user, a Mayor that has a City (the city is a foreign key to a City model).
You do not want to override the classic user model.
You want to create a Mayor from the admin
You want the newly created Mayor to be part of the Staff (Staff User can access the admin, but they are not SuperUser)
In the models.py file, where the User is defined, create a new class that inherits the User
Create the migration file: ./manage.py makemigrations
or docker-compose run django_container_name ./manage.py makemigrations
Run the migration: ./manage.py migrate
or docker-compose run django_container_name ./manage.py migrate
Check 1: Run the show migration command to see the new one
./manage.py showmigrations
In the admin.py, where the User model is added admin.register(User)
Add the following:
Check 1: You now see the Mayor in the admin. But all the forms (form to add a Mayor, form to change/update a Mayor) are the same as for the regular User.
You need to know the fields you want to display. username
, password1
and password2
are default fields of the "user admin add form"
In our case:
Check 1: You now see the city field in the "add new mayor" form.
You can manually set a user to be part of the staff, you can also override the save method of the AddForm so it changes the property is_staff to True
Then use this form in the MyMayorAdmin class:
Check 1: When I create a mayor, I see that it is staff user in the detail view of the mayor.
In the admin.py when you declared your MyMayorAdmin class, you can choose the fields you want to display in the form by overriding the add_fieldsets
property. You can readme more about add_fieldset
First create a form that inherits the AddUserForm and override the save method (see ):