  .. code-block:: pycon
  
      >>> from django.db.models import Avg, F, Window
      >>> Movie.objects.annotate(
      >>>     avg_rating=Window(
      >>>         expression=Avg('rating'),
      >>>         partition_by=[F('studio'), F('genre')],
      >>>         order_by='released__year',
      >>>     ),
      >>> )
  
  This allows you to check if a movie is rated better or worse than its peers.
  
  You may want to apply multiple expressions over the same window, i.e., the
  same partition and frame. For example, you could modify the previous example
 
</s> remove     >>>     avg_rating=Window(
    >>>         expression=Avg('rating'),
    >>>         partition_by=[F('studio'), F('genre')],
    >>>         order_by='released__year',
    >>>         frame=ValueRange(start=-12, end=12),
    >>>     ),
    >>> )
 </s> add     ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...         frame=ValueRange(start=-12, end=12),
    ...     ),
    ... ) </s> remove     >>>     avg_rating=Window(
    >>>         expression=Avg('rating'),
    >>>         partition_by=[F('studio'), F('genre')],
    >>>         order_by='released__year',
    >>>         frame=RowRange(start=-2, end=2),
    >>>     ),
    >>> )
 </s> add     ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...         frame=RowRange(start=-2, end=2),
    ...     ),
    ... ) </s> remove     >>>    'partition_by': [F('studio'), F('genre')],
    >>>    'order_by': 'released__year',
    >>> }
 </s> add     ...    'partition_by': [F('studio'), F('genre')],
    ...    'order_by': 'released__year',
    ... } </s> remove     >>>     avg_rating=Window(
    >>>         expression=Avg('rating'), **window,
    >>>     ),
    >>>     best=Window(
    >>>         expression=Max('rating'), **window,
    >>>     ),
    >>>     worst=Window(
    >>>         expression=Min('rating'), **window,
    >>>     ),
    >>> )
 </s> add     ...     avg_rating=Window(
    ...         expression=Avg('rating'), **window,
    ...     ),
    ...     best=Window(
    ...         expression=Max('rating'), **window,
    ...     ),
    ...     worst=Window(
    ...         expression=Min('rating'), **window,
    ...     ),
    ... ) </s> remove         >>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ... AUTHORITY["EPSG","4326"]]')
 </s> add         >>> wkt = 'GEOGCS["WGS 84", DATUM["WGS_1984, ... AUTHORITY["EPSG","4326"]]'
    ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...     ),
    ... )
  .. code-block:: pycon
  
      >>> from django.db.models import Avg, F, Window
      >>> Movie.objects.annotate(
    ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...     ),
    ... )
    ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...     ),
    ... )
    ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...     ),
    ... )
    ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...     ),
    ... )
    ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...     ),
    ... )
    ...     avg_rating=Window(
    ...         expression=Avg('rating'),
    ...         partition_by=[F('studio'), F('genre')],
    ...         order_by='released__year',
    ...     ),
    ... )
  
  This allows you to check if a movie is rated better or worse than its peers.
  
  You may want to apply multiple expressions over the same window, i.e., the
  same partition and frame. For example, you could modify the previous example
keep keep keep keep replace replace replace replace replace replace keep keep keep keep keep
 Refs #34140 -- Corrected rst code-block and various formatting issues in docs. 
docs/ref/models/expressions.txt