summaryrefslogtreecommitdiff
path: root/tutorial_6_django_shell_queries/slides.md
blob: 954cbffed998d7d0eb43e283dcb7ff2c8b01d776 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
Tutorial: Django Shell and Django Database Query
=====================================
[Demonstration time: 7 mins 0 secs (0.8333 ~ 83%) | Total time: 8 mins 24 s]
------------

Slide 1 [00:00 | 00:08]
------------
Title Slide
**Django Shell and Creating Django Database Query**

Slide 2 [00:08 | 00:16]
--------------

**Learning Objectives**

In this tutorial, we will learn to;
  - Use the django shell
  - Create a django query

Slide 3 [00:08 | 00:24]
---------------

**System Requirements**
  - Ubuntu 16.10
  - Python 3.5 or higher version
  - python3.4-venv
  
Slide 4 [00:08 | 00:32]
---------------

**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 [00:12 | 00:44]
------------

**What is the Django Shell**

 - The Django shell is a Python shell configured for our Django Project.
 - We can execute our project code in this shell.
 - We can manipulate our app models in this shell

Demonstration [03:00 | 3:44]
------------

**Start the Django Shell**

In the terminal, run the command

*python manage.py shell*

(For script creator: Here there will be certain flow, i.e activating virtual env and cd to the project directory etc..
 Also, flow will be better if before starting any activity we have an initial statement like: Let us now create/see/run....etc..)

(For script creator: small description why this command and not python.)

Run the following Django Query

    >>> from blog.models import Blog, Article
    >>> Blog.objects.all()
    >>> <QuerySet [<Blog: Blog object>]> # Output
  
Explanation: This django query returns all the instances of the Blog model.
Returns a QuerySet similar to a list

Now let's add a new blog

    >>> 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 database table 'Blog'

Now we access model field values via Python attributes.

    >>> b.id
    >>> b.name
    >>> b.created_on

Change values

    >>> b.name = 'This is a blog from shell!'
    >>> b.save()
    >>> b.name

    >>> Blog.objects.all()

In output we see, <QuerySet [<Blog: Blog object>....]>
  -  Blog object in the output is not a good representation for the object.

Let us fix this in the models.py
 - Add \__str__() method to the models

       class Blog(models.model):
        .
        .
          def __str__(self):
             return self.name

       class Article(models.model):
           .
           .
           def __str__(self):
               return self.title


Lets add a custom method to the blog model

    class Blog(models.model):
    .
    .
       def was_created_recently(self):
           return self.created_on >= timezone.now() - datetime.timedelta(days=1)

In the shell:

    >>> b.was_created_recently()

Demonstration [01:30 | 05:14]
--------------

**Filtering Entries with Django Queries**

    >>> from blog.models import Blog, Article
    >>> b2 = Blog.objects.filter(id=2)
    >>> b2
  
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[0].id
    >>> 2 # Output
  
Try sorting the Blog objects based on created_on year

    >>> current_year = timezone.now().year
    >>> Blog.objects.filter(created_on__year=current_year)

Explanation: This query will give all the blog instances with created_on the current year


Demonstration [01:00 | 06:14]
-------------
** methods for queryset returned**

    >>> blogs =  Blog.objects.all()
    >>> blogs.count()
    >>> blogs. <press TAB here to show > available methods

Demonstration [01:30 | 07:44]
-----------------

**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()
    
(For script creator: Now again we can do a filter or get with id =2, get will throw an Exception that Blog.DoesNotExists)

Remaining conluding slides with the Assignment [00:40 | 8:24]
----------------------------------------------