Skip to content

Commit

Permalink
Apply "code-block" directive to unmarked code snippets under "ref/mod…
Browse files Browse the repository at this point in the history
…els". Manual process. Re-run blacken-docs.
  • Loading branch information
jvzammit committed Nov 13, 2022
1 parent e5692df commit 76fc58a
Show file tree
Hide file tree
Showing 10 changed files with 874 additions and 432 deletions.
17 changes: 10 additions & 7 deletions docs/ref/models/conditional-expressions.txt
Expand Up @@ -13,18 +13,21 @@ also be combined and nested like other :doc:`expressions <expressions>`.
The conditional expression classes
==================================

We'll be using the following model in the subsequent examples::
We'll be using the following model in the subsequent examples:

.. code-block:: python

from django.db import models


class Client(models.Model):
REGULAR = 'R'
GOLD = 'G'
PLATINUM = 'P'
REGULAR = "R"
GOLD = "G"
PLATINUM = "P"
ACCOUNT_TYPE_CHOICES = [
(REGULAR, 'Regular'),
(GOLD, 'Gold'),
(PLATINUM, 'Platinum'),
(REGULAR, "Regular"),
(GOLD, "Gold"),
(PLATINUM, "Platinum"),
]
name = models.CharField(max_length=50)
registered_on = models.DateField()
Expand Down
20 changes: 13 additions & 7 deletions docs/ref/models/constraints.txt
Expand Up @@ -110,17 +110,19 @@ This method must be implemented by a subclass.
A :class:`Q` object or boolean :class:`~django.db.models.Expression` that
specifies the check you want the constraint to enforce.

For example, ``CheckConstraint(check=Q(age__gte=18), name='age_gte_18')``
For example, ``CheckConstraint(check=Q(age__gte=18), name="age_gte_18")``
ensures the age field is never less than 18.

.. admonition:: Oracle

Checks with nullable fields on Oracle must include a condition allowing for
``NULL`` values in order for :meth:`validate() <BaseConstraint.validate>`
to behave the same as check constraints validation. For example, if ``age``
is a nullable field::
is a nullable field:

.. code-block:: python

CheckConstraint(check=Q(age__gte=18) | Q(age__isnull=True), name='age_gte_18')
CheckConstraint(check=Q(age__gte=18) | Q(age__isnull=True), name="age_gte_18")

.. versionchanged:: 4.1

Expand All @@ -141,9 +143,11 @@ ensures the age field is never less than 18.
Positional argument ``*expressions`` allows creating functional unique
constraints on expressions and database functions.

For example::
For example:

.. code-block:: python

UniqueConstraint(Lower('name').desc(), 'category', name='unique_lower_name_category')
UniqueConstraint(Lower("name").desc(), "category", name="unique_lower_name_category")

creates a unique constraint on the lowercased value of the ``name`` field in
descending order and the ``category`` field in the default ascending order.
Expand Down Expand Up @@ -171,9 +175,11 @@ date.
A :class:`Q` object that specifies the condition you want the constraint to
enforce.

For example::
For example:

.. code-block:: python

UniqueConstraint(fields=['user'], condition=Q(status='DRAFT'), name='unique_draft_user')
UniqueConstraint(fields=["user"], condition=Q(status="DRAFT"), name="unique_draft_user")

ensures that each user only has one draft.

Expand Down
28 changes: 16 additions & 12 deletions docs/ref/models/database-functions.txt
Expand Up @@ -125,21 +125,25 @@ Accepts a list of at least two field names or expressions and returns the
greatest value. Each argument must be of a similar type, so mixing text and
numbers will result in a database error.

Usage example::
Usage example:

class Blog(models.Model):
body = models.TextField()
modified = models.DateTimeField(auto_now=True)
.. code-block:: pycon

class Comment(models.Model):
body = models.TextField()
modified = models.DateTimeField(auto_now=True)
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
>>> class Blog(models.Model):
... body = models.TextField()
... modified = models.DateTimeField(auto_now=True)
...

>>> class Comment(models.Model):
... body = models.TextField()
... modified = models.DateTimeField(auto_now=True)
... blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
...

>>> from django.db.models.functions import Greatest
>>> blog = Blog.objects.create(body='Greatest is the best.')
>>> comment = Comment.objects.create(body='No, Least is better.', blog=blog)
>>> comments = Comment.objects.annotate(last_updated=Greatest('modified', 'blog__modified'))
>>> blog = Blog.objects.create(body="Greatest is the best.")
>>> comment = Comment.objects.create(body="No, Least is better.", blog=blog)
>>> comments = Comment.objects.annotate(last_updated=Greatest("modified", "blog__modified"))
>>> annotated_comment = comments.get()

``annotated_comment.last_updated`` will be the most recent of ``blog.modified``
Expand Down Expand Up @@ -301,7 +305,7 @@ returned when this timezone is active will be the same as above except for:

Each ``lookup_name`` above has a corresponding ``Extract`` subclass (listed
below) that should typically be used instead of the more verbose equivalent,
e.g. use ``ExtractYear(...)`` rather than ``Extract(..., lookup_name='year')``.
e.g. use ``ExtractYear(...)`` rather than ``Extract(..., lookup_name="year")``.

Usage example:

Expand Down

0 comments on commit 76fc58a

Please sign in to comment.