Welcome everyone to this series of lectures on discussing data interaction.
As we previously mentioned, we need to be able to create or insert read updates and delete information
from our database.
Let's begin by discussing creation and insertion into the database.
Inserting new data into a school table is super easy with changing models.
Recall, since the models are represented by a Python class, we can easily create a new instance of
the class object in Python defined the attributes and then call the dot safe method on that class to
create an insert call to the school database.
Alternatively, you can use the built in the objects, the create method to both creates and save the
new data entry in a single line that saves you the two step process of having to create and then save
the changes.
You can just call the objects to create, and they'll both create and push that save into the actual
database.
In instances where you want to create multiple new data entries in bulk in a single call, you can use
the objects that bulk underscore create method to pass any list of newly created objects.
So let's explore these three methods of creating new data entries of a model.
The first of the simplest where we actually separate out into two steps creating the object and then
saving it to the database, then we do it in one step with objects to create, and then we can do multiple
at a time with objects.
Stop, bulk, create.
Keep in mind, we haven't taught you how to read in data from a database.
Still, just have to take my word for it.
For now that these entries are actually being inserted into the database, in the next lecture, we'll
discuss reading information in order to confirm the entries we do here.
OK, let's head over to our code editor.
All right, so here I am inside Visual Studio code and we've set up our models and we've already ran
those migrations.
Now, how can I actually explore and play around with the model and the database?
One way would be to build an entire website and have forms on the front end and then run those and check
through admin on the backend to see if those changes were actually updated.
Now that's a lot of work, just to begin to understand the way the model calls.
That is, the method calls on a model object actually operate and interact with the database.
So instead, when you want to just play around with some of the files already created with Django Django
inside of Managed API actually offers you a shell command, and that shell command allows you to then
interact with the Python files without having to go through the entire process of building out your
website, which keep in mind, we will eventually do.
Clearly, we're not going to be actually editing these models at the terminal command line, but for
right now, just to get an idea of how different methods work, interact and operate, we'll be doing
this at the terminal.
So in order to run your actual shell, what you do is at my site.
We're going to call again managed pie and then one of its different sub commands in the same way it
has a start up Sub Command is going to be the shell command.
Go ahead and run python, manage that pie shell and you'll see something that looks like this essentially
a connection between python and shell.
Now, keep in mind, you may not actually be using a python.
I have an enhanced, interactive python that I've configured of this terminal for demonstration purposes
only.
What's nice about this is it performs a little bit of syntax highlighting and also directly reports
what the input was.
And then later on, you'll see what the output was.
So I can say something like Prince, hello.
And you can see that syntax highlighting enter and then I get to see the output.
Hello.
It's very similar to the way Jupyter Notebook operates.
If you have some familiarity of that, do not worry at all.
If you just see a normal Python interpreter, that's totally OK.
All the code is going to work the same as well.
So let's go ahead and continue.
First, I want to be able to use this patient model.
So what I'm going to say is the following I'm going to say.
From.
Office dot models import patient because inside this office folder is in its PDF file and models that
pie.
So I'm going to say from office, not models, import patients.
I read that and now I should be able to actually create a patient's object.
So let's do that, let's say.
In fact, let's just call this object, Carl.
And we're going to create a new patient called Carl.
And we'll see patients and then this war after the find the first name.
So say first name is going to be equal to.
Let's say, Karl.
Last name is going to be equal to.
And then I'm going to say Smith.
It's probably capitalize those, but not a big deal right now.
Only constraint is to make sure they're only 30 characters and then the age.
Remember, the age has to be an integer, so we're gonna say age is, let's say, 30.
OK, so that's technically number one, I have this Python object that exists, but Carl himself first
named Carl Larson Smith age 30.
This actual data wrote instance is not yet inserted into my SQL database, so that means I can still
do any sort of checks I want with Carl.
So, for example, I could say, Carl, check your age and it reports back.
Or I could check.
I could do something like Carl age.
Is it less than 20?
And it would give me false.
Hopefully, it gives you the idea that you can do still a lot of functionality on the Python object
without having to actually commit it to the database so you can run any sort of checks or validations
yourself using Python code.
Later on, we'll talk about validators they can actually build into the model.
But keep in mind, you can do all sorts of checks and different interactions with this Python object
before you committed to the database.
Now, let's say Karl looks like the ages, OK, and we are ready to end up pushing this to our actual
database.
Then what we can say here is Karl, and you simply call that safe enter.
And this itself just ran the insert command to now be part of the database here and SQLite three.
You may be wondering where does this that safe method call actually come from?
It comes from the inherited models that model class.
OK, so this is the simplest way to actually create a new entry into our database.
You create an instance of that actual model class like patient.
You could grab attributes from it and check it out.
But at the end of day, you were going to then save it.
So again, it's two steps creating it and then saving it.
Now, let's imagine you already know that you want to create Karl with first name, last name and age
or some other patient.
Idon't want to have to separate this out into two calls here.
You're just going to accept it right off the bat as correct.
Well, what you can do there is the following you can say.
Patient and then you're going to call dot objects dot create.
And this is a shorthand for then accepting the attribute calls and calling them, say automatically.
So we say patients to objects that create.
And here you can then say first name.
Let's say she, Susan.
Last name.
I'm also going to say Smith, and let's make her age.
Forty.
OK.
So I run this command and we can see a new patient has been created and this patient, Susan Smith,
has also now been saved to our actual database, so she exists inside this DB sequel like three.
And let's do just one more patient using objects to create.
And I just cleared this, you can also clear this with seals in Python.
But don't worry about that right now if you're running a normal python.
And what we're going to do here is the following I'm going to say again patients remember I already
imported this and say objects that create.
And then what I'm going to do here again is, say, first name is equal to and let's just have this
one be called me me.
Last name, we're going to have this one be called, say, bullies, and let's have the HP 36.
OK, go ahead and hit.
Enter here and you'll notice that the number actually reflects the index position.
So this is technically the third patient.
So we have index.
One was the very first patient we created with creating the object, then saving it to a Susan Smith.
And then three again, here is Mimi Bolus.
All right.
So the final thing I want to show you is the ability to create multiple patients at once.
So let me clear this and expand a little bit here.
Since this is going to take multiple lines, but sometimes you want to bulk create, how does that actually
work?
You're going to use the following.
We'll be using the bulk creates method before we actually run it, though, I do want to point out the
documentation because this is such a powerful tool to just do a bunch of entries at once.
You may want to take into account the caveats here.
So there's a couple of caveats of different types of databases or relationships between tables that
this won't technically work for.
So, for example, it doesn't work with many too many relationships.
We'll explain more on that later when we actually create tables that have relationships between each
other.
But just keep in mind, there are some caveats here to using this tool.
For most basic use cases, though, it should be fine to just pass any list of your new objects themselves.
So we're going to do here is I'm going to create a separate list of new patients and then use entry
the objects that bull create to then just quickly insert that list.
And keep in mind, it's generally only one query, no matter how many objects there are.
You could technically use the batch size argument to try to fix this if it was a huge amount of entries,
but for most cases, this should be totally fine.
So we're going to do here or show you how to do it with patients.
So let's make a list of patients.
I'm going to say my list.
Is equal to that, let's create some patients here, so we'll say patient, remember, I already imported
this.
So the first name of this patient?
Is going to be, let's say, Adam, and then let's say, last name.
Is Smith?
And then we'll say age is 40.
So that's our first patient.
And let's add one more patient to this list.
We're going to say patients.
And let's say the first name here.
Well, I guess since we're talking about Adam Smith or also put in his buddy Karl Marx here.
Not really, but you get the idea and then we'll say last name Marx, and then we'll say age is also
40.
OK, and then remember to close off that list.
So I just created a new list, my list, and it has two patients in that list.
Adam Smith and Karl Marx.
Maybe they can duke it out there inside the database.
And then what we're going to do is we're going to call entry.
The objects are actually patient that objects and then bulk create.
So I'm going to call patients that.
Objects and similar to I can call create.
Now I'm going to call bulk create and then here can pass in my list.
Run that, and you can see it created those patient objects, and it reports them back essentially that
they got inserted.
Next, we have to actually see how this worked, so we would like to be able to read the results that
are in the database, not just create and say everything's OK.
And in fact, you notice that on some of these doesn't actually tell you the idea.
None.
So we want to explore all of that and more in the next lecture where we talk about reading information
from a database.
But as a review, remember, you can actually create a new object, then call save on it.
You can say objects that create and then actually create a single object and save it in one command.
Or if you want to quickly put in a lot, you can say bulk underscore.
Create.
OK, let's head over to the next lecture where we actually read in the data that we just created inserted
into the database.
I'll see you there.