#100DaysOfWeb in Python Transcripts
Chapter: Days 45-48: Build a simple Django app
Lecture: Playing with the ORM in Django's shell

Login or purchase this course to watch this video and the rest of the course contents.
0:02 One final note about the last video
0:05 when we did to make migration.
0:07 It only worked because Quotes was now in installed apps.
0:10 And I already put it in installed apps
0:13 when we did the configuration video.
0:15 So just keep that in mind.
0:17 In order for the migrate to work
0:19 you need to have the app in the installed apps
0:22 and you have to go to make
0:24 migration first to make the SQL file.
0:28 Okay, let's move on to the Django shell
0:30 and work a little bit with Django's ORM.
0:34 ORM standing for Object Relational Mapping.
0:39 So, we cannot just open a Python shell right here.
0:44 We need to load in Django's environment.
0:47 So in order to work with Django's objects
0:49 you need to do python manage.py shell.
0:56 And now we can import a model we just created.
1:03 And we have to specify an absolute path.
1:10 You will see that quotes is indeed a model
1:13 and now we can create some quotes.
1:15 And to make a quote, you can use
1:18 the quote model as a class.
1:21 And pass at the values in the constructor.
1:41 Now we have the quote object
1:43 but it's not stored in the database.
1:45 We have to do that explicitly with quote.save
1:51 Now we can retrieve objects
1:53 with the quote objects all.
1:58 And here we see one quote.
2:00 You remember in the last video that I mention
2:03 the __str__ method to make the objects more readable?
2:07 Well, here you see that in action.
2:09 If I wouldn't have defined a __str__
2:11 I would just see object and would not
2:13 really see what the object was.
2:15 So defining that __str__ with self.quote - self.author
2:21 now gives me a reasonable return
2:23 and I can see what the queries set holds.
2:26 Now let's add two more quotes.
2:38 And now I have two.
2:42 And for this one I will add a cover.
2:54 So now I have three
2:57 Can get them all like this
3:00 or I can filter on all queries.
3:09 That works too or I can even filter with
3:14 any object that contains the letter K.
3:19 K is insensitive.
3:22 I guess me too.
3:24 And for R, I want to get Roosevelt.
3:28 Right, now, how can we edit a quote.
3:32 Editing is as simple as retrieving the quote.
3:36 So the Roosevelt one.
3:40 And just overwrite it's attributes
3:42 and save it back.
3:44 So let's add a cover for example.
3:59 And save it.
4:01 It's interesting, assigning the
4:03 filter object to quote, got me actually
4:06 a query set and not an individual object.
4:09 So when I did this
4:12 I got a list object or a query set.
4:19 And you can use slicing to get the first one.
4:22 So now I get an actual object.
4:28 So now I can do the assignment of the cover.
4:32 And now I can do the save.
4:35 I query it again, you shouldn't see any difference
4:41 because the __str__ method only prints the
4:43 quote and the author but if I look at the cover
4:53 it did store it to the database.
4:55 Ah, to delete similar.
5:06 We're using the delete method, on the quote.
5:14 And now I'm back to 2.
5:16 So that's add, edit and delete using the Django ORM.
5:20 And we will see this again in the views
5:22 when we start to retrieve
5:23 and edit data from the database
5:25 and load them into our templates.
5:27 It would be good at this point to
5:28 start a shell and try out ORM for yourself.
5:32 Again, you can use quote objects all
5:37 or filter to retrieve.
5:39 You can use slicing to get the object.
5:43 Once you have it save to a variable
5:46 you can modify it as if it was a class instance.
5:49 So you can just assign to the attributes.
5:57 Then you have to use save to
5:59 persist it to the database.
6:01 For delete, use delete
6:03 and there's not an extra save needed.
6:06 That will come in to the database, as we saw here.
6:11 So that's the basics of the ORM.
6:13 I mean the querying goes way further
6:15 so it's worth it to read up Django's ORM.
6:18 But at the very basic level
6:19 this is how you interact with objects
6:21 using Django ORM and the Django shell.
6:24 And that concludes the video's for day 1.