Django: Getting Started Transcripts
Chapter: Users and Account Management
Lecture: Adding the user profile to Django admin
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Alright, that's the profile object and it's automatic creation. Let's update the Django admin as well.
0:08
Opening up people admin.py, replacing the boiler plate, just like the other admin files I'm going to need the admin module.
0:26
Line 3 is something new, I'm importing the original user admin objects so that I can extend it. I've renamed it as base user admin to avoid confusion.
0:37
In order to register my new version of the user admin, I'll need the user object so that gets imported on line 4.
0:44
And as I'm creating an admin model for the reader object, I'll need the reader object which gets pulled in on line 6.
1:00
This is the admin class for the reader profile instead of inheriting from admin model, this inherits from stacked in line.
1:10
As the name implies, this is to create a form that is in line with another objects Admin page.
1:16
The two fields here configure the reader in line objects so that it is based on a reader object and indicates that you can't delete this directly.
1:25
You don't want user objects kicking around without associated profiles. This is the new version of the user admin.
1:42
It extends the old one and first off it registers the reader in line as an in line form. Now, this is where the helpfulness of the Django admin starts
1:52
to get in your way. Registering reader in line will make it show up on the create page as well as on the edit page. If you do it on the create page,
2:03
Django admin will try to create a reader for you and you'll end up with a race condition inside of that signal I showed you.
2:11
So to get around that line 16 overrides the admin models get in line instances method. This method is what returns the in lines for the class.
2:25
On line 17 I check whether or not the object exists yet. If it doesn't then just return an empty list.
2:33
That means there will be no in lines on the create screen. On line 20 I'm actually returning the default which will be the in lines registered
2:43
on line 14. Got it? One more time, remember when you create a user you put in the user name and password first and then you can add all the attributes.
2:54
The reader in line form is only going to show up in the edit form which is that form where you can edit the attributes,
3:00
not where you create the base user. The last change I have to make here is that the user object had already been
3:14
registered in the admin, the old one that is. So I have to unregistered the old one and replace it with my newly customized user admin.
3:26
The last thing to do is to migrate the objects into the database.
3:46
Let's go to the browser and check this out. And now I'll create a user notice that the in line doesn't show up here.
4:02
The user has been created. Now I'm on the edit page where I can fill in the attributes of the profile and here's my user profile in line at the bottom
4:13
I can select the user's favorite books and save them out.