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