blob: 70203abb15b75896720dffa9dbe674f0286a33c1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
Slide 1 [00:08 | 00:08]
------------
Title Slide
**Creating Views and Routing URLs**
Slide 2 [00:12 | 00:20]
--------------
**Learning Objectives**
In this tutorial, we will learn to;
- Use the django shell
- Create a django query
Slide 3 [00:11 | 00:31]
---------------
**System Requirements**
- Ubuntu 16.10
- Python 3.5 or higher version
- python3.4-venv
Slide 4 [00:11 | 00:42]
---------------
**Pre-requisites**
In order to follow this tutorial, you need to know;
- how to create models in django
- If not, see the relevant django tutorial on http://spoken-tutorial.org
Slide 5
------------
**What is the Django Shell**
The Django shell is a tool to help developers to
- Manipulate Django objects in a python command line environment
- Fetch and view objects in a python command line environment
Demonstration
------------
**Start the Django Shell**
In your command line environment, run the command
*python manage.py shell*
Demonstration
-------------
**Using the Django Shell**
Run the following Django Query
>>> from blog.models import Blog, Article
>>> Blog.objects.all()
>>> <QuerySet [<Blog: Blog object>]> # Output
Explanation: This django query displays all the instances of the Question model as a list.
Now let's add a new blog using the Django shell
>>> from django.utils import timezone
>>> b = Blog(name="My Second Blog", created_on=timezone.now())
>>> b.save()
Explanation:
- We create an instance of Blog and add values to it's fields (name and creation date)
- We then save the object, which means Django writes this information to the SQL table 'Blog'
Demonstration
--------------
**Filtering Entries with Django Queries**
Close and restart the django shell using the command
*python manage.py shell*
>>> from blog.models import Blog, Article
>>> b2 = Blog.objects.filter(id=2)
>>> b2
>>> <QuerySet [<Blog: Blog object>]> # Output
>>> get_b2 = Blog.objects.get(id=2)
>>> get_b2
>>> <QuerySet [<Blog: Blog object>]> # Output
Explanation: This query will give all the Blog instances with id 1, since the id is unique we get only one result. You can make sure that the id is 2 by running
>>> b2.id
>>> 2 # Output
Try sorting the Blog objects based on created_on year
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> Question.objects.filter(created_on__year=current_year)
Explanation: This query will give all the blog instances with created_on year
Demonstration
-----------------
**Queries for related Objects**
>>> b1 = Blog.objects.get(id=1)
>>> b1.articles_set.all()
Show output
Explanation: Since every article is related to a Blog (because of the ForeignKey relationship), we can always get all Articles related to a particular blog by using the "_set" suffix
>>> a1 = Article.objects.get(id=1)
>>> b = a1.blog
>>> b.id
Explanation: You can always access the information of the Blog object through it's related Article object as shown above
You can also delete objects from shell
>>> b2.delete()
|