diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
index af307fa..ec9a9e6 100644
|
a
|
b
|
For example, suppose you have these models::
|
| 891 | 891 | toppings = models.ManyToManyField(Topping) |
| 892 | 892 | |
| 893 | 893 | def __str__(self): # __unicode__ on Python 2 |
| 894 | | return "%s (%s)" % (self.name, ", ".join(topping.name |
| 895 | | for topping in self.toppings.all())) |
| | 894 | return "%s (%s)" % ( |
| | 895 | self.name, |
| | 896 | ", ".join(topping.name or topping in self.toppings.all()), |
| | 897 | ) |
| 896 | 898 | |
| 897 | 899 | and run:: |
| 898 | 900 | |
| … |
… |
This is meant as a shortcut to boilerplatish code. For example::
|
| 1636 | 1638 | This pattern gets quite unwieldy as the number of fields in a model goes up. |
| 1637 | 1639 | The above example can be rewritten using ``get_or_create()`` like so:: |
| 1638 | 1640 | |
| 1639 | | obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', |
| 1640 | | defaults={'birthday': date(1940, 10, 9)}) |
| | 1641 | obj, created = Person.objects.get_or_create( |
| | 1642 | first_name='John', |
| | 1643 | last_name='Lennon', |
| | 1644 | defaults={'birthday': date(1940, 10, 9)}, |
| | 1645 | ) |
| 1641 | 1646 | |
| 1642 | 1647 | Any keyword arguments passed to ``get_or_create()`` — *except* an optional one |
| 1643 | 1648 | called ``defaults`` — will be used in a :meth:`get()` call. If an object is |
diff --git a/docs/ref/utils.txt b/docs/ref/utils.txt
index 43d082e..71404d7 100644
|
a
|
b
|
Sample usage::
|
| 318 | 318 | >>> feed.add_item( |
| 319 | 319 | ... title="Hello", |
| 320 | 320 | ... link="http://www.holovaty.com/test/", |
| 321 | | ... description="Testing." |
| | 321 | ... description="Testing.", |
| 322 | 322 | ... ) |
| 323 | 323 | >>> with open('test.rss', 'w') as fp: |
| 324 | 324 | ... feed.write(fp, 'utf-8') |
| … |
… |
escaping HTML.
|
| 630 | 630 | |
| 631 | 631 | So, instead of writing:: |
| 632 | 632 | |
| 633 | | mark_safe("%s <b>%s</b> %s" % (some_html, |
| 634 | | escape(some_text), |
| 635 | | escape(some_other_text), |
| 636 | | )) |
| | 633 | mark_safe("%s <b>%s</b> %s" % ( |
| | 634 | some_html, |
| | 635 | escape(some_text), |
| | 636 | escape(some_other_text), |
| | 637 | )) |
| 637 | 638 | |
| 638 | 639 | You should instead use:: |
| 639 | 640 | |
| 640 | 641 | format_html("{} <b>{}</b> {}", |
| 641 | | mark_safe(some_html), some_text, some_other_text) |
| | 642 | mark_safe(some_html), |
| | 643 | some_text, |
| | 644 | some_other_text, |
| | 645 | ) |
| 642 | 646 | |
| 643 | 647 | This has the advantage that you don't need to apply :func:`escape` to each |
| 644 | 648 | argument and risk a bug and an XSS vulnerability if you forget one. |
| … |
… |
escaping HTML.
|
| 661 | 665 | ``args_generator`` should be an iterator that returns the sequence of |
| 662 | 666 | ``args`` that will be passed to :func:`format_html`. For example:: |
| 663 | 667 | |
| 664 | | format_html_join('\n', "<li>{} {}</li>", ((u.first_name, u.last_name) |
| 665 | | for u in users)) |
| | 668 | format_html_join( |
| | 669 | '\n', "<li>{} {}</li>", |
| | 670 | ((u.first_name, u.last_name) for u in users) |
| | 671 | ) |
| 666 | 672 | |
| 667 | 673 | .. function:: strip_tags(value) |
| 668 | 674 | |
diff --git a/docs/topics/db/queries.txt b/docs/topics/db/queries.txt
index ba026c8..12e1750 100644
|
a
|
b
|
will return ``Blog`` objects that have an empty ``name`` on the ``author`` and
|
| 522 | 522 | also those which have an empty ``author`` on the ``entry``. If you don't want |
| 523 | 523 | those latter objects, you could write:: |
| 524 | 524 | |
| 525 | | Blog.objects.filter(entry__authors__isnull=False, |
| 526 | | entry__authors__name__isnull=True) |
| | 525 | Blog.objects.filter(entry__authors__isnull=False, entry__authors__name__isnull=True) |
| 527 | 526 | |
| 528 | 527 | Spanning multi-valued relationships |
| 529 | 528 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| … |
… |
select all blogs that contain entries with both *"Lennon"* in the headline
|
| 559 | 558 | and that were published in 2008 (the same entry satisfying both conditions), |
| 560 | 559 | we would write:: |
| 561 | 560 | |
| 562 | | Blog.objects.filter(entry__headline__contains='Lennon', |
| 563 | | entry__pub_date__year=2008) |
| | 561 | Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008) |
| 564 | 562 | |
| 565 | 563 | To select all blogs that contain an entry with *"Lennon"* in the headline |
| 566 | 564 | **as well as** an entry that was published in 2008, we would write:: |
| 567 | 565 | |
| 568 | | Blog.objects.filter(entry__headline__contains='Lennon').filter( |
| 569 | | entry__pub_date__year=2008) |
| | 566 | Blog.objects.filter(entry__headline__contains='Lennon').filter(entry__pub_date__year=2008) |
| 570 | 567 | |
| 571 | 568 | Suppose there is only one blog that had both entries containing *"Lennon"* and |
| 572 | 569 | entries from 2008, but that none of the entries from 2008 contained *"Lennon"*. |
diff --git a/docs/topics/testing/tools.txt b/docs/topics/testing/tools.txt
index f9e302a..cc1a7e6 100644
|
a
|
b
|
and contents::
|
| 1652 | 1652 | class EmailTest(TestCase): |
| 1653 | 1653 | def test_send_email(self): |
| 1654 | 1654 | # Send message. |
| 1655 | | mail.send_mail('Subject here', 'Here is the message.', |
| | 1655 | mail.send_mail( |
| | 1656 | 'Subject here', 'Here is the message.', |
| 1656 | 1657 | 'from@example.com', ['to@example.com'], |
| 1657 | | fail_silently=False) |
| | 1658 | fail_silently=False, |
| | 1659 | ) |
| 1658 | 1660 | |
| 1659 | 1661 | # Test that one message has been sent. |
| 1660 | 1662 | self.assertEqual(len(mail.outbox), 1) |