From the course: Django Essential Training
Using admin for data creation and manipulation - Django Tutorial
From the course: Django Essential Training
Using admin for data creation and manipulation
- [Instructor] We've already created a table for notes, but if you're curious enough to go to the Django admin interface, you'll notice that nothing really changed. Why is that? The same way we didn't have to create a user model, it was just there. We didn't have to configure it to appear on the Django interface. When we are creating a new model, we need to do it ourselves. So let's go back to the notes app and open a file called admin.py. This is where we go into add which models can be displayed, and thus, modified via the Django admin interface. First, let's create a class and call it NotesAdmin. This class should inherit from admin.ModelAdmin. Let's add pass here because we don't want any additional configuration on this admin model. Now what we need to do is import from this folder. Let's import models, and on the bottom of the file, we're going to register that that model is attached to this admin model. So let's write admin.site.register, then models.Notes, and NotesAdmin. Okay, that's it. Let's go back to the admin and refresh it. There you go. Now we can see that the notes model is available on the admin interface. Let's use the add button here to create a new note. Let's title, My First Note, and then, Django is so amazing. Let's save this. Okay, we have our first note created. One thing that isn't really nice is that it is listed as this Notes Object One. This is fine for now, but if we have a long list of notes, how can we tell which one is which? Let's go back to the admin class. Instead of pass here, we can pass list_display, which is going to be a tuple, and let's pass title here. Let's save this. It restarted. And now, if we refresh here, there, instead of having this ugly name, we have the title of the note being displayed here. The default configuration of admin also allows that all fields can be changed by all users. However, we can edit the admin model class and start adding some specialized logic. We can remove some fields from being edited. We can allow only staff users to write notes. There's a lot we can do, the sky's the limit. Django admin is highly configurable.