Django: Getting Started Transcripts
Chapter: Django Admin
Lecture: Setting up an Admin model test

Login or purchase this course to watch this video and the rest of the course contents.
0:32 All right, a few more imports up top here than I've used in the past These are used to be able to create a user and to do that
0:40 look up fields function I just spoke about. Then because I'm testing them, I need the author and author admin models.
0:49 Seeing as I just spoke out against hard coded strings, I've added some constants here for the admin's user name, email, and password.
1:02 Before testing, I'm going to need a super user account. I'm going to create that here inside of setup.
1:08 I'm only going to write one test so it doesn't really matter. But this is the best practice.
1:14 Speaking of better practice, I believe in previous chapters I skipped the whole call the parents method thing.
1:20 You can get away with it depending on what you're doing, but using Super here is the safest bet. On line 17 I'm creating an instance object of
1:30 the admin site which I'll use later to reference the admin site. The lines I just added create a new user.
1:48 Notice that a user isn't created like any other object. The user's query manager has a special method called create user.
1:57 This is necessary because you don't store the password in plain text. The create user method takes care of the whole encryption thing.
2:05 Unfortunately it only takes three parameters. User name, email, and password. This means if you want to create staff or super users you have to do that
2:15 manually after it's been created. And because these are only proxy objects, I have to save it against to write that information down to the database.


Talk Python's Mastodon Michael Kennedy's Mastodon