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.

Changing How Models Are Edited

00:00 Changing How Models Are Edited. You can customize more than just the change list page. The screens used to add or change an object are based on a ModelForm. Django automatically generates the form based on the model being edited.

00:17 You can control which fields are included, as well as their order, by editing the fields option. Once more, modify your PersonAdmin object, adding a fields attribute.

00:37 The Add and Change pages for Person now put the first_name attribute before the last_name attribute, even though the model itself specifies the other way around.

00:48 ModelAdmin.get_form() is responsible for creating the ModelForm for your object. You can override this method to change the form. Here, you can see the method being added to PersonAdmin.

01:12 Now, as you can see onscreen, when the Add or Change page is displayed, the label of the first_name field is customized. Changing the label might not be sufficient to prevent vampires from registering as students.

01:27 If you don’t like the ModelForm that Django admin created for you, then you can use the form attribute to register a custom form.

01:34 Here some more changes have been made to core/admin.py.

01:53 This code enforces additional validation on the Person Add and Change pages. ModelForm objects have a rich validation mechanism. In this case, the first_name field is being checked against the name "Spike". A ValidationError prevents students with this name from registering.

02:20 By changing or replacing the ModelForm object, you can fully control the appearance and validation of the pages you use to add or change object pages.

02:38 With more control over the data that’s presented and how it’s edited and changed, the next area to investigate is changing the templates which control the appearance of the admin area pages, and that’s what you’ll be seeing in the next section.

Become a Member to join the conversation.