Django: Getting Started Transcripts
Chapter: Django Admin
Lecture: Creating Admin views for your models
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
All right, you're all set. You've got a super user account. You've seen the Django admin, you've updated a model object. Now, what?
0:08
The next step is to get your own models into the Django admin. This is done in a similar fashion to how models are declared.
0:17
Instead of using the apps model.py file, you use the apps admin.py file instead of inheriting from models.model, you inherit the model admin instead.
0:28
You then register this new class against the model that it represents and that's all you need, but you can do more.
0:36
You can customize the model admin class to change what is shown in the admin and what can be done to the model.
0:43
Let's go into pycharm and I'll show you how it's done. When I ran the command that created the catalog app. Django created a sub admin.py file for me.
0:53
Let me open it up and now I'm just going to make a couple additions. This is the module you'll need to do just about everything.
1:07
Like I did with the models module rather than import the classes underneath. I import the module itself and use it directly.
1:15
Not sure why I do it this way, But it seems to be how most Django examples do it. So I must have picked up the habit along the way,
1:25
as I'm going to be admitting the book and author models. I'm going to need them. So here I have imported them.
1:36
These three lines are the minimum you need to have your model show up in the Django admin. The register decorator takes a parameter which is the model.
1:45
This admin model is going to administer. In this case it's the author object. The author admin class inherits from the admin.model admin.
1:56
You thought me trying to say model a lot in the same sentence was confusing. Of course you can name this whatever you'd like but best practice is
2:04
to name it the same as the model being administered plus the word admin.
2:09
Like I've done here, declaring the class and wrapping it with the registration decorator,
2:14
means that Django can now find this object and we'll use it in the web interface. Let's go to the web and take a look at what's there.
2:25
With the code in place. The author object now shows up in a section called catalog named after the app.
2:31
Notice that it paralyzes your model name automatically. This can be a problem. I'll talk about it later. If I click on the author's link,
2:41
I get the authors change list. Reminiscent of the rebel, this isn't a particularly pretty listing.
2:48
In fact, this is exactly what you would see in the rebel if you printed out one of these objects. The number in brackets here is the objects id,
2:58
Thankfully there are ways of making this more useful. Let's go back to pycharm.