Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Providing Links to Other Objects

00:00 Providing Links to Other Object Pages.

00:05 It’s quite common for objects to reference other objects through the use of foreign keys. You can point list_display at a method that returns an HTML link. Inside core/admin.py, modify the CourseAdmin class as follows.

00:32 This code causes the Course change list to have three columns: the course name, the year in which the course was offered, and a link displaying the number of students in the course.

01:04 When you click 2 Students, it takes you to the Person change list page with a filter applied. The filtered page shows only those students in Psych 101, Buffy and Willow.

01:15 Xander didn’t make it to university. The example code uses reverse() to look up a URL in the Django admin. You can look up any admin page using the following naming convention.

01:29 This name structure breaks down as follows. admin is the namespace, app is the name of the app, model is the model object, page is the Django admin page type.

01:44 For the view_students_link() seen in the previous code, the link used is admin:core_person_changelist to get a reference to the change list page of the Person object in the core app.

01:58 Here are the available URL names: _changelist, _add, _history, _delete, and _change. You can filter the change list page by adding a query string to the URL.

02:14 This query string modifies the QuerySet used to populate the page. In the example shown here, the query string filters the Person list to only show those objects that have a matching value in Person.course.

02:28 Here, you can see the effect of this. The course__id appears as a 2 in the query string and the matches are shown below. These filters support QuerySet field lookups using double underscores (__).

02:41 You can access attributes of related objects as well as use filter modifiers like __contains and __startswith. You can find the full details on what you can accomplish with the list_display attribute in the Django admin documentation.

02:58 As you can imagine, there are situations where you can have too much information present in the admin screen, and that’s where filters come in. They’re covered in the next section.

Become a Member to join the conversation.