diff options
Diffstat (limited to 'tutorial_6_django_shell_queries/slides.md')
-rw-r--r-- | tutorial_6_django_shell_queries/slides.md | 117 |
1 files changed, 81 insertions, 36 deletions
diff --git a/tutorial_6_django_shell_queries/slides.md b/tutorial_6_django_shell_queries/slides.md index 70203ab..d690298 100644 --- a/tutorial_6_django_shell_queries/slides.md +++ b/tutorial_6_django_shell_queries/slides.md @@ -1,9 +1,14 @@ -Slide 1 [00:08 | 00:08] +Tutorial: Django Shell and Django Database Query +===================================== +[Demonstration time: 7 mins 00 secs (0.8333 ~ 83%) | Total time: 8 mins 24 s] +------------ + +Slide 1 [00:00 | 00:08] ------------ Title Slide -**Creating Views and Routing URLs** +**Django Shell and Creating Django Database Query** -Slide 2 [00:12 | 00:20] +Slide 2 [00:08 | 00:16] -------------- **Learning Objectives** @@ -12,7 +17,7 @@ In this tutorial, we will learn to; - Use the django shell - Create a django query -Slide 3 [00:11 | 00:31] +Slide 3 [00:08 | 00:24] --------------- **System Requirements** @@ -20,7 +25,7 @@ Slide 3 [00:11 | 00:31] - Python 3.5 or higher version - python3.4-venv -Slide 4 [00:11 | 00:42] +Slide 4 [00:08 | 00:32] --------------- **Pre-requisites** @@ -29,28 +34,28 @@ 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 +Slide 5 [00:12 | 00:44] ------------ **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 + - 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 +Demonstration [03:00 | 3:44] ------------ **Start the Django Shell** -In your command line environment, run the command +In the terminal, run the command *python manage.py shell* -Demonstration -------------- +(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..) -**Using the Django Shell** +(For script creator: small description why this command and not python.) Run the following Django Query @@ -59,48 +64,93 @@ Run the following Django Query >>> <QuerySet [<Blog: Blog object>]> # Output -Explanation: This django query displays all the instances of the Question model as a list. +Explanation: This django query returns all the instances of the Blog model. -Now let's add a new blog using the Django shell +Now let's add a new blog >>> from django.utils import timezone - >>> b = Blog(name="My Second Blog", created_on=timezone.now()) + >>> 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 +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 shell: + >>> b2.was_created_recently() + +Demonstration [01:30 | 05:14] -------------- **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 + >>> b2[0].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) + >>> Blog.objects.filter(created_on__year=current_year) Explanation: This query will give all the blog instances with created_on year -Demonstration + +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** @@ -122,10 +172,5 @@ You can also delete objects from shell >>> b2.delete() - - - - - - - +Remaining conluding slides with the Assignment [00:40 | 8:24] +---------------------------------------------- |