H a n d s o n, p r o j e c t b a s e d
Chapter 18 When you click Save
Download 4.21 Mb. Pdf ko'rish
|
Python Crash Course, 2nd Edition
- Bu sahifa navigatsiya:
- The Django Shell
392
Chapter 18 When you click Save, you’ll be brought back to the main admin page for entries. Here, you’ll see the benefit of using text[:50] as the string rep- resentation for each entry; it’s much easier to work with multiple entries in the admin interface if you see only the first part of an entry rather than the entire text of each entry. Make a second entry for Chess and one entry for Rock Climbing so we have some initial data. Here’s a second entry for Chess: In the opening phase of the game, it’s important to bring out your bishops and knights. These pieces are powerful and maneu- verable enough to play a significant role in the beginning moves of a game. And here’s a first entry for Rock Climbing: One of the most important concepts in climbing is to keep your weight on your feet as much as possible. There’s a myth that climbers can hang all day on their arms. In reality, good climb- ers have practiced specific ways of keeping their weight over their feet whenever possible. These three entries will give us something to work with as we continue to develop Learning Log. The Django Shell With some data entered, we can examine that data programmatically through an interactive terminal session. This interactive environment is called the Django shell, and it’s a great environment for testing and trouble- shooting your project. Here’s an example of an interactive shell session: (ll_env)learning_log$ python manage.py shell u >>> from learning_logs.models import Topic >>> Topic.objects.all() The command python manage.py shell , run in an active virtual environ- ment, launches a Python interpreter that you can use to explore the data stored in your project’s database. Here, we import the model Topic from the learning_logs.models module u. We then use the method Topic.objects.all() to get all the instances of the model Topic ; the list that’s returned is called a queryset. We can loop over a queryset just as we’d loop over a list. Here’s how you can see the ID that’s been assigned to each topic object: >>> topics = Topic.objects.all() >>> for topic in topics: ... print(topic.id, topic) Getting Started with Django 393 ... 1 Chess 2 Rock Climbing We store the queryset in topics , and then print each topic’s id attribute and the string representation of each topic. We can see that Chess has an ID of 1, and Rock Climbing has an ID of 2. If you know the ID of a particular object, you can use the method Topic .objects.get() to retrieve that object and examine any attribute the object has. Let’s look at the text and date_added values for Chess: >>> t = Topic.objects.get(id=1) >>> t.text 'Chess' >>> t.date_added datetime.datetime(2019, 2, 19, 1, 55, 31, 98500, tzinfo= We can also look at the entries related to a certain topic. Earlier we defined the topic attribute for the Entry model. This was a ForeignKey , a con- nection between each entry and a topic. Django can use this connection to get every entry related to a certain topic, like this: u >>> t.entry_set.all() To get data through a foreign key relationship, you use the lowercase name of the related model followed by an underscore and the word set u. For example, say you have the models Pizza and Topping , and Topping is related to Pizza through a foreign key. If your object is called my_pizza , representing a single pizza, you can get all of the pizza’s toppings using the code my_pizza .topping_set.all() . We’ll use this kind of syntax when we begin to code the pages users can request. The shell is very useful for making sure your code retrieves the data you want it to. If your code works as you expect it to in the shell, you can expect it to work properly in the files within your project. If your code generates errors or doesn’t retrieve the data you expect it to, it’s much eas- ier to troubleshoot your code in the simple shell environment than within the files that generate web pages. We won’t refer to the shell much, but you should continue using it to practice working with Django’s syntax for access- ing the data stored in the project. n o t e Each time you modify your models, you’ll need to restart the shell to see the effects of those changes. To exit a shell session, press ctrl -D; on Windows, press ctrl -Z and then press enter . |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling