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


Talk Python's Mastodon Michael Kennedy's Mastodon