The Miracle of Creation5:17 with Kenneth Love
Sometimes we need to create a model instance outside of a model form. Or, sometimes, we need to create several model instances at once. Let's see how the ORM handles both of these situations.
When you're using
get_or_create(), you'll probably want to assign the object that comes back and the boolean for whether or not it was created in some variables.
course, created = Course.objects.get_or_create(title="REST API Basics") will divide up the values for you, into the two variables. If you don't need the
created value, though, you can just throw it away with an underscore:
course, _ = Course.objects.get_or_create(title="REST API Basics").
So far when we've needed to make a new instance in the database, 0:00 we've used either the admin or a model form. 0:03 Both of these are excellent ways and probably the way you'll create almost all 0:05 of your model instances when you're using Django. 0:09 But 99% probability or 0:11 not, you need to know about three other handy ORM methods for creating records. 0:13 So sometimes you just need to make a model instance really quick. 0:19 You know the data is good, you don't wanna bother with a form, 0:22 you just gotta stick a thing into the database. 0:25 Django's ORM give us a really quick way to handle that using the create model. 0:27 I've already got courses imported, I need to import the user, 0:32 so django.contrib.auth.models import user. 0:37 Okay, cool. 0:41 So I want to make a new course for myself really quick. 0:42 So I'm gonna go ahead and get my user record out, so 0:45 get username equals kennethlove, I know that's me. 0:50 All right, cool, and now I wanna make a course. 0:55 So let's do course equals Course.objects.create 0:58 teacher equals teacher, me subject equals Python. 1:02 Big surprise right there and the title's gonna be Django Basics. 1:07 All right, and so now if I look at the id of the course, it's 24. 1:16 Now, I notice I didn't have to go and make a course instance. 1:20 I didn't have a call save, I didn't have to go fetch it afterwards. 1:23 Create is a wonderful shortcut, it inserts the record into the database and 1:26 then gives you back the record. 1:31 So course is now that course that I created in the database. 1:34 So that's really cool. 1:37 This only works for creating one thing though. 1:38 If you need to make several items, you're probably like a loop, loop,loop, but no, 1:40 don't make a loop. 1:44 You can but ideally you don't. 1:45 There's actually something that's a little bit easier to do which is a bulk create. 1:47 So we can do course.objects.bulk_create. 1:51 And you always pass both create and iterable of some kind. 1:59 So in this case, we're sending in a list and 2:02 inside that iterable, you have instances of the model that are not saved. 2:03 So we'll do a course where the teacher equals to teacher, 2:09 title equals Django Forms and the subject equals Python. 2:13 And then we'll do another one where the teacher equal teacher. 2:17 And just to get a little bit [INAUDIBLE] Django ORM and subect equals Python. 2:25 All right. And then we'll close our loop and 2:33 we'll close our call to bulk create. 2:35 And cool, we got back a list, 2:38 we could've assign that to an object if we wanted to a variable, but it's cool. 2:40 So now if we do a Course.objects.filter title contains Django. 2:44 Then cool, we've got Lacey's customizing Django templates and 2:53 we then have my own Django basic's string of forms and the current Django ORM. 2:57 So that's great, it created all the courses for us at once. 3:02 If we're doing this in a view with Django debug toolbar, 3:05 we'd only see a single query being run for inserting both of those records. 3:08 We wouldn't get two queries, we'd get one which is awesome. 3:12 So yey for being efficient. 3:16 You should also be able to figure out how to do this with a loop or 3:17 a list comprehension but you don't have to of course. 3:20 Speaking with efficiency, there's no reason to add records that we 3:22 don't need to add or to throw exceptions recklessly. 3:26 So just like we have the get object or 404 to save ourselves from having to catch 3:30 a missing record and throw a 404 when it doesn't exist. 3:35 We also have the get or 3:38 create method, well, let's quickly move through possible record data. 3:39 Get or create checks to see if a record exists using 3:43 all of the attributes that we give it. 3:46 If it does exist, we get that record back. 3:48 If it doesn't exist, it will create the record and give it back to us. 3:50 It also gives us back a boolean value of whether or not the record was created. 3:54 So for instance, let's try this, 3:58 course.objects.get or create, 4:02 teacher equals teacher, title equals Django forms. 4:07 And I get a back course and I get back false because it wasn't being created, 4:14 it already existed. 4:18 Let's change this a little bit here and let's go with the Django REST Framework. 4:20 All right. 4:27 This doesn't exist but some day it will, right? 4:30 [INAUDIBLE] And we get back to the course and 4:33 we get back true because it was created, all right. 4:35 Out of all these methods, I probably use get or create more than anything else. 4:39 I imagine you will too, so enjoy it. 4:43 I found that over the years I tend to use create a lot 4:48 when I'm playing with ideas in the shell. 4:50 It's a really handy way of building stuff quickly just to make sure that a model 4:52 method works or that I can select records the way I think I can. 4:55 Bulk create comes in really handy for 4:58 a lot of start up scripts like when a project first starts. 5:01 And get or create is one that is pretty much indispensable for 5:04 a lot of applications like when you're creating tags or categories and 5:07 want to only have one record of each tag. 5:10 I'm sure you'll find all sorts of ways that these three methods save you 5:13 time and trouble. 5:15
You need to sign up for Treehouse in order to download course files.Sign up