Registering Models With the Admin
There’s not a lot going on here right now because we haven’t registered our app’s models—Venue
, MyClubUser
and Event
—with the admin. Registering and configuring a model for the admin is done by adding the models to the events
app’s admin.py
file (changes in bold):
# \myclub_root\events\admin.py
1 from django.contrib import admin
2 from .models import Venue, MyClubUser, Event
3
4 admin.site.register(Venue)
5 admin.site.register(MyClubUser)
6 admin.site.register(Event)
We’ve added four lines of code to our admin.py
file:
- Line 2. Import the
Venue
,MyClubUser
andEvent
models. - Line 4. Register the
Venue
model with the admin. - Line 5. Register the
MyClubUser
model with the admin. - Line 6. Register the
Event
model with the admin.
Save the file, refresh your browser, and your admin index page should now look like Figure 7-3. You can see there is now an Events group under the Authentication and Authorization group.
Figure 7-3: Once registered, the Venue
, MyClubUser
and Event
models display in the admin.
When you register a model with the admin, you can use the admin to add, edit and delete model records. Now we’ve registered the Event
, MyClubUser
and Venue
models with the admin, we can use the admin to manage venues, users and events for our club.
Django’s admin is designed to be simple and intuitive for non-technical users. Each model registered with the admin has a change list and an edit form. If you click on Venues in the admin index, this will open the venues change list (Figure 7.4). Your venue list might be empty—I have added some records so you can see the default change list for the model.
Figure 7-4: The Venues change list. Click “Add Venue” to add a new venue.
The change list is a table containing all the rows in the database for that particular model. To open an edit form to edit a record, click on the record title (the venue name in this example). To open a blank edit form to add a new record, click the “Add Venue” button on the top right.