Loading docs/ref/models/querysets.txt +9 −4 Original line number Diff line number Diff line Loading @@ -921,8 +921,10 @@ For example, suppose you have these models:: toppings = models.ManyToManyField(Topping) def __str__(self): # __unicode__ on Python 2 return "%s (%s)" % (self.name, ", ".join(topping.name for topping in self.toppings.all())) return "%s (%s)" % ( self.name, ", ".join(topping.name or topping in self.toppings.all()), ) and run:: Loading Loading @@ -1669,8 +1671,11 @@ This is meant as a shortcut to boilerplatish code. For example:: This pattern gets quite unwieldy as the number of fields in a model goes up. The above example can be rewritten using ``get_or_create()`` like so:: obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)}) obj, created = Person.objects.get_or_create( first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)}, ) Any keyword arguments passed to ``get_or_create()`` — *except* an optional one called ``defaults`` — will be used in a :meth:`get()` call. If an object is Loading docs/ref/utils.txt +14 −8 Original line number Diff line number Diff line Loading @@ -317,7 +317,7 @@ Sample usage:: >>> feed.add_item( ... title="Hello", ... link="http://www.holovaty.com/test/", ... description="Testing." ... description="Testing.", ... ) >>> with open('test.rss', 'w') as fp: ... feed.write(fp, 'utf-8') Loading Loading @@ -629,7 +629,8 @@ escaping HTML. So, instead of writing:: mark_safe("%s <b>%s</b> %s" % (some_html, mark_safe("%s <b>%s</b> %s" % ( some_html, escape(some_text), escape(some_other_text), )) Loading @@ -637,7 +638,10 @@ escaping HTML. You should instead use:: format_html("{} <b>{}</b> {}", mark_safe(some_html), some_text, some_other_text) mark_safe(some_html), some_text, some_other_text, ) This has the advantage that you don't need to apply :func:`escape` to each argument and risk a bug and an XSS vulnerability if you forget one. Loading @@ -658,8 +662,10 @@ escaping HTML. ``args_generator`` should be an iterator that returns the sequence of ``args`` that will be passed to :func:`format_html`. For example:: format_html_join('\n', "<li>{} {}</li>", ((u.first_name, u.last_name) for u in users)) format_html_join( '\n', "<li>{} {}</li>", ((u.first_name, u.last_name) for u in users) ) .. function:: strip_tags(value) Loading docs/topics/db/queries.txt +3 −6 Original line number Diff line number Diff line Loading @@ -519,8 +519,7 @@ will return ``Blog`` objects that have an empty ``name`` on the ``author`` and also those which have an empty ``author`` on the ``entry``. If you don't want those latter objects, you could write:: Blog.objects.filter(entry__authors__isnull=False, entry__authors__name__isnull=True) Blog.objects.filter(entry__authors__isnull=False, entry__authors__name__isnull=True) Spanning multi-valued relationships ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Loading Loading @@ -556,14 +555,12 @@ select all blogs that contain entries with both *"Lennon"* in the headline and that were published in 2008 (the same entry satisfying both conditions), we would write:: Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008) Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008) To select all blogs that contain an entry with *"Lennon"* in the headline **as well as** an entry that was published in 2008, we would write:: Blog.objects.filter(entry__headline__contains='Lennon').filter( entry__pub_date__year=2008) Blog.objects.filter(entry__headline__contains='Lennon').filter(entry__pub_date__year=2008) Suppose there is only one blog that had both entries containing *"Lennon"* and entries from 2008, but that none of the entries from 2008 contained *"Lennon"*. Loading docs/topics/testing/tools.txt +4 −2 Original line number Diff line number Diff line Loading @@ -1703,9 +1703,11 @@ and contents:: class EmailTest(TestCase): def test_send_email(self): # Send message. mail.send_mail('Subject here', 'Here is the message.', mail.send_mail( 'Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False) fail_silently=False, ) # Test that one message has been sent. self.assertEqual(len(mail.outbox), 1) Loading Loading
docs/ref/models/querysets.txt +9 −4 Original line number Diff line number Diff line Loading @@ -921,8 +921,10 @@ For example, suppose you have these models:: toppings = models.ManyToManyField(Topping) def __str__(self): # __unicode__ on Python 2 return "%s (%s)" % (self.name, ", ".join(topping.name for topping in self.toppings.all())) return "%s (%s)" % ( self.name, ", ".join(topping.name or topping in self.toppings.all()), ) and run:: Loading Loading @@ -1669,8 +1671,11 @@ This is meant as a shortcut to boilerplatish code. For example:: This pattern gets quite unwieldy as the number of fields in a model goes up. The above example can be rewritten using ``get_or_create()`` like so:: obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)}) obj, created = Person.objects.get_or_create( first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)}, ) Any keyword arguments passed to ``get_or_create()`` — *except* an optional one called ``defaults`` — will be used in a :meth:`get()` call. If an object is Loading
docs/ref/utils.txt +14 −8 Original line number Diff line number Diff line Loading @@ -317,7 +317,7 @@ Sample usage:: >>> feed.add_item( ... title="Hello", ... link="http://www.holovaty.com/test/", ... description="Testing." ... description="Testing.", ... ) >>> with open('test.rss', 'w') as fp: ... feed.write(fp, 'utf-8') Loading Loading @@ -629,7 +629,8 @@ escaping HTML. So, instead of writing:: mark_safe("%s <b>%s</b> %s" % (some_html, mark_safe("%s <b>%s</b> %s" % ( some_html, escape(some_text), escape(some_other_text), )) Loading @@ -637,7 +638,10 @@ escaping HTML. You should instead use:: format_html("{} <b>{}</b> {}", mark_safe(some_html), some_text, some_other_text) mark_safe(some_html), some_text, some_other_text, ) This has the advantage that you don't need to apply :func:`escape` to each argument and risk a bug and an XSS vulnerability if you forget one. Loading @@ -658,8 +662,10 @@ escaping HTML. ``args_generator`` should be an iterator that returns the sequence of ``args`` that will be passed to :func:`format_html`. For example:: format_html_join('\n', "<li>{} {}</li>", ((u.first_name, u.last_name) for u in users)) format_html_join( '\n', "<li>{} {}</li>", ((u.first_name, u.last_name) for u in users) ) .. function:: strip_tags(value) Loading
docs/topics/db/queries.txt +3 −6 Original line number Diff line number Diff line Loading @@ -519,8 +519,7 @@ will return ``Blog`` objects that have an empty ``name`` on the ``author`` and also those which have an empty ``author`` on the ``entry``. If you don't want those latter objects, you could write:: Blog.objects.filter(entry__authors__isnull=False, entry__authors__name__isnull=True) Blog.objects.filter(entry__authors__isnull=False, entry__authors__name__isnull=True) Spanning multi-valued relationships ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Loading Loading @@ -556,14 +555,12 @@ select all blogs that contain entries with both *"Lennon"* in the headline and that were published in 2008 (the same entry satisfying both conditions), we would write:: Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008) Blog.objects.filter(entry__headline__contains='Lennon', entry__pub_date__year=2008) To select all blogs that contain an entry with *"Lennon"* in the headline **as well as** an entry that was published in 2008, we would write:: Blog.objects.filter(entry__headline__contains='Lennon').filter( entry__pub_date__year=2008) Blog.objects.filter(entry__headline__contains='Lennon').filter(entry__pub_date__year=2008) Suppose there is only one blog that had both entries containing *"Lennon"* and entries from 2008, but that none of the entries from 2008 contained *"Lennon"*. Loading
docs/topics/testing/tools.txt +4 −2 Original line number Diff line number Diff line Loading @@ -1703,9 +1703,11 @@ and contents:: class EmailTest(TestCase): def test_send_email(self): # Send message. mail.send_mail('Subject here', 'Here is the message.', mail.send_mail( 'Subject here', 'Here is the message.', 'from@example.com', ['to@example.com'], fail_silently=False) fail_silently=False, ) # Test that one message has been sent. self.assertEqual(len(mail.outbox), 1) Loading