#100DaysOfWeb in Python Transcripts
Chapter: Days 69-72: Django REST Framework
Lecture: Writing a custom django-admin command

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right so we have quotes set up and the next thing I'm going to do is is to load in some quotes to have some data to work with.
0:08 And in Django the manage.py command you can write customized scripts to run via that interface. And in this video we're going to
0:18 parse a CSV file with a bunch of quotes and load them into our ORM into our quotes table. So I got this file prepared which is quote author genre
0:30 and we're going to parse this with the CSV module and load them into our database. The first thing we want to do is to
0:37 make the require directory structure. So in my quotes app so again this is the project root folder and every Django app has its own directory
0:48 with the requires files. And in order to use Django commands we need to have a management commands folder structure.
0:59 So I'll use the minus p to make that recursively then I'll cd into it and let's make a script import_quotes.py. Alright here is the code.
1:15 In this is a Django REST framework lesson I'm not going to go into too much detail but just go quickly over it. So I'll do the standard library imports
1:25 the external modules. We need the user model and the BaseCommand which is the class we're going to inherit from to write our own command.
1:35 And we need requests and that actually is a good point because I did not have that installed because it was not in the requirements.
1:47 Let's do that quickly now. Then we import the quote model. We define where the quotes.csv is. We set a default user and a default for maximum quotes.
2:08 Those are actually the two arguments we can call the script with. So we give it a user to be associated with the quotes that we're importing
2:17 and we define the maximum quotes we want to import 'cause in that CSV there are a lot of quotes and I just want to have 20 by default
2:25 to make it manageable for our lesson. Two rules, the new command has to inherit from BaseCommand and it needs to override the handle method.
2:38 And this is actually the meat of the command. So let me just explain. First we look at the quote model to see how many rows there are in the database
2:49 and if it's greater than zero, we bail out because we don't want to import the quotes again. And an alternative way to do that is actually
2:57 to set unique on the table on the quote column but we didn't do that so I have to kind of safeguard that here.
3:07 Then we retrieve the command line arguments so username and limit and of course we check if the username is in the database and if not, we bail out.
3:18 Same for the max quotes we need to make sure it's an integer and if not, we bail out. And then we will use the requests
3:25 library to get the inspirational quotes which again is this CSV on my GitHub repo. We split the lines and actually split lines here
3:37 so we get the text attribute on the request response object. Strip any white spaces from both sides and we split it by a new line
3:47 which we too can better then doing it like this. We're going to define the headers called author and genre. And we are going to use csv.DictReader
3:58 and we pass it in the lines and headers. And we say that the delimiter is not comma but semicolon. Then we make a quotes list
4:09 and we loop over the reader object convert it into a list. And I do that to use slicing to get the maximum quotes. The first row is the header
4:19 so I'm starting my slice at one the second row. I go up to next quotes which by default is 20. And as this is exclusive, I add one.
4:29 So I take the first 20 rows after my header. So going up til row 21. Then I'm going to make a quote object in which we saw in the previous lessons
4:43 the way to make objects in Django admin in the model is to define instances of classes. So here I make quote instance and I'll give a keyword arguments
4:57 so the quote, author, and the user. I keep a list of quotes. I could also do quote save here. And it would be committed to the database.
5:08 Another nice technique is if you have many records to insert is to do a bulk_create. So you can do a Quote.objects.bulk_create
5:16 and you could give it a list of quote objects. And that's it. Then we print a message saying we create n quotes in this case 20 and that should be it.
5:29 So again any logic goes into handle and any command has to inherited from BaseCommand. So let's try that out. So we use the manage.py we give it a -h.
5:46 Sorry, I need to give it the command. Here we see our command line, arguments and help messages we defined. So we need the user name and a limit.
6:00 Let's give it some bad data. Okay, log is not in the database as expected. Well we add that wrong 'cause we even get to the second one.
6:15 So let's correct that and do limit. ABC. And we need a number let's actually do 30. And let's create a 20. That's not good. Okay.
6:36 I was using the constant here and I need to use the actually variable. First of all, we can not do this again hopefully.
6:49 Yup, not an empty DB, currently exiting. Perfect. Just to make sure we actually have 30 quotes let's just print the count before exiting.
7:12 And we actually have 30. Yeah, that's great. Save this, run the server, go to the page, and boom.
7:31 A bunch of quotes. I can of course add quote because I'm logged in as pybites. Actually going to log out.
7:40 I can not add a quote, so I have to log into add quotes. Can edit them. Great. Alright, so now it's time to start
7:49 building our API with Django REST framework in the next video.

Talk Python's Mastodon Michael Kennedy's Mastodon