Skip to content

Commit

Permalink
Merge pull request DjangoGirls#1237 from brondsem/queryset
Browse files Browse the repository at this point in the history
Change more output samples to current QuerySet output
  • Loading branch information
RachellCalhoun authored Mar 9, 2018
2 parents 9aa5e5e + fa156f0 commit 3a4e1b0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions en/django_orm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Now we can finally create our post:
{% filename %}command-line{% endfilename %}
```python
>>> Post.objects.create(author=me, title='Sample title', text='Test')
<Post: Sample title>
```

Hurray! Wanna check if it worked?
Expand All @@ -125,15 +126,15 @@ A big part of QuerySets is the ability to filter them. Let's say we want to find
{% filename %}command-line{% endfilename %}
```python
>>> Post.objects.filter(author=me)
[<Post: Sample title>, <Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>]
<QuerySet [<Post: Sample title>, <Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>]>
```

Or maybe we want to see all the posts that contain the word 'title' in the `title` field?

{% filename %}command-line{% endfilename %}
```python
>>> Post.objects.filter(title__contains='title')
[<Post: Sample title>, <Post: 4th title of post>]
<QuerySet [<Post: Sample title>, <Post: 4th title of post>]>
```

> **Note** There are two underscore characters (`_`) between `title` and `contains`. Django's ORM uses this rule to separate field names ("title") and operations or filters ("contains"). If you use only one underscore, you'll get an error like "FieldError: Cannot resolve keyword title_contains".
Expand All @@ -144,7 +145,7 @@ You can also get a list of all published posts. We do this by filtering all the
```python
>>> from django.utils import timezone
>>> Post.objects.filter(published_date__lte=timezone.now())
[]
<QuerySet []>
```

Unfortunately, the post we added from the Python console is not published yet. But we can change that! First get an instance of a post we want to publish:
Expand All @@ -166,7 +167,7 @@ Now try to get list of published posts again (press the up arrow key three times
{% filename %}command-line{% endfilename %}
```python
>>> Post.objects.filter(published_date__lte=timezone.now())
[<Post: Sample title>]
<QuerySet [<Post: Sample title>]>
```


Expand All @@ -177,24 +178,25 @@ QuerySets also allow you to order the list of objects. Let's try to order them b
{% filename %}command-line{% endfilename %}
```python
>>> Post.objects.order_by('created_date')
[<Post: Sample title>, <Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>]
<QuerySet [<Post: Sample title>, <Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>]>
```

We can also reverse the ordering by adding `-` at the beginning:

{% filename %}command-line{% endfilename %}
```python
>>> Post.objects.order_by('-created_date')
[<Post: 4th title of post>, <Post: My 3rd post!>, <Post: Post number 2>, <Post: Sample title>]
<QuerySet [<Post: 4th title of post>, <Post: My 3rd post!>, <Post: Post number 2>, <Post: Sample title>]>
```


### Chaining QuerySets

You can also combine QuerySets by **chaining** them together:

```
```python
>>> Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
<QuerySet [<Post: Post number 2>, <Post: My 3rd post!>, <Post: 4th title of post>, <Post: Sample title>]>
```

This is really powerful and lets you write quite complex queries.
Expand Down
2 changes: 1 addition & 1 deletion en/django_templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ It works! But we want the posts to be displayed like the static posts we created

![Figure 13.3](images/step3.png)

Have you noticed that we used a slightly different notation this time (`{{ post.title }}` or `{{ post.text }})`? We are accessing data in each of the fields defined in our `Post` model. Also, the `|linebreaksbr` is piping the posts' text through a filter to convert line-breaks into paragraphs.
Have you noticed that we used a slightly different notation this time (`{{ post.title }}` or `{{ post.text }}`)? We are accessing data in each of the fields defined in our `Post` model. Also, the `|linebreaksbr` is piping the posts' text through a filter to convert line-breaks into paragraphs.


## One more thing
Expand Down

0 comments on commit 3a4e1b0

Please sign in to comment.