Getting Started with Django Admin

The admin is a very useful tool for developers to use when working with django. It is super easy to setup, and get your models registered with it so you can start CRUDing data.
Download HD Version next video: Organize Your Data Better in Admin Detail Page

resources

The Django admin site

documents/admin.py

admin.site.register(Document)

documents/admin.py

class DocumentAdmin(admin.ModelAdmin)
    pass

admin.site.register(Document, DocumentAdmin)

documents/admin.py

@admin.register(Document)
class DocumentAdmin(admin.ModelAdmin)
    pass

One of the things that sells Django to people is the Admin that comes with Django. It's a very powerful tool for developers, and one that we really need to know how to use. It's fairly simple to get it up and running and in this video we're gonna go over that, and then in a few future videos we're going to go over how to customize it.

To get started with the Admin, if you actually generate a fresh project, it already quote-on-quote "works." We already have a project that we're actually using and it's the one we've used in previous episodes for you being able to upload a file using Django REST framework. However, for these series of videos, we're going to not worry so much about that and just try to access the data from the database in the Admin. Again, with a freshly-generated site, the Admin is turned on, and that's the case for this application.

To make sure the Admin is activated for you and usable, you need to open up your Settings file, and then install the apps, make sure you have django.contrib.admin included and installed apps. This is so that Django picks up all the things that needs to pick up for you to be able to access and use it. You also need to open up your URL's file and make sure you're importing Admin from Django Contrib, and then you have the Admin URL and are including the Admin URL's as well.

From there, you need to run a migration, and then create a superuser on the command line using manage.py create superuser and follow the prompts. That gets the Admin actually working for you. If we'll open up our browser and the /admin, our local host :9000/admin, you'll see we have the Django administration login view. If we'll go ahead and log in, we have our normal Admin in the top right you'll have your View Site which'll go to the home page, Change Password, and Logout. Those are basic administrative settings.

You have Django administration in the top left, and then as you'll look down you'll see where you actually have groups of data and these are divided into Django apps. So authentication authorization comes from the Django contrib auth, and auth token actually comes from Django rest framework token. If you'll remember from the previous video though, that we have a document model and a document app, but those aren't actually listed here. So let's go ahead and do what it takes so that we can see it in the Admin.

First, if we'll look in our Models, you have our class document. Nothing's changed, it's title in a file field, and the model name is "document." That's the most important part for this exercise. If we'll open up the admin.py folder in Documents, you'll see we're importing Admin from Django contrib. The Admin actually looks for the admin.py file in every single app that you have, so it can automatically load that information. What we need to do is register our model with the admin site so that it can be used.

We just do that with admin.site, register, and then we pass it our model. This tells our Admin site that we wanna be able to view our document model and the data it contains in our Admins. We'll close out of this, and we'll go ahead and open up our Admin and refresh, we see we now have documents. And when we click on it, we have Document object, Document object, da-da-da-da-dah, you open them up, and we have our Title and our File.

And as you can see we're actually seeing details about this document, and then in the bottom you can see we can Save, we can Save and continue editing, we can Save and add another object. And then we can also go ahead and delete this object as well, so we have all our basic CRUD operations available to us. If we'll go back to our list, if you'll look in the top right there, there's Add Document, that's to be able to add a new Document object to our database. Unfortunately, seeing Document object, Document, object, Document object does not really help us any, because what's happening is Django Admin is displaying the unicode value of the object.

So in order to fix that, we need to go into our Documents model, we need to create a new method called str, and we'll just return self.title. And in Python 2.7 that would be underscore, underscore, unicode, underscore, underscore. That returns back title as the string or unicode representation of this object. So we go back into our Admin and refresh, you see we now have the actual title of each of our documents displayed nicely to us. That's really all there is to setting up our Admin.

Let's go ahead and take a look at our admin.py file and look at a couple of other ways to go ahead and register our model. That'll get us set up for our next video. If we'll go in to our admin.py, we'll create a new class and call it "document admin", and we're going to inherit from model admin, and we're just gonna put "pass." This is going to allow us to customize our Admin view of our model in many different ways.

For now, though, this just allows us an opportunity to say, "Hey, this document is going to be associated "with this Document Admin model," so that when it does all the instantiation, it knows where to start. So in our register command, we'll just add in Document admin, so now our Admin site knows, "Hey, for the document model we need to use "the Document admin," and it's gonna work just the same as it has been because we're just inheriting from model admin and we're not changing anything.

So we'll look at this in our browser and refresh. You see there's no change, it still just works. The final way I wanna look at registering the model with our Admin, is to go ahead and delete our admin.site.register, and then just add a decorator of admin.register to our Document admin class. This is actually something that was just added in a recent version of Django, so you don't have to have all your registers at the bottom of your page, you can just say, "Hey, I wanna register the Document "model now with the Document admin," and you're good to go. So it keeps it a little bit cleaner on what's going on where. We'll open up in our browser again and hit "refresh." There's no change. So we can go into our documents, and you can see we have data.

Thank you for watching and please join us on our next video, where we go a little more in-depth into the Django admin.

comments powered by Disqus