Django 2.0

Feature Overview

Features

  • Simplified URL routing syntax
  • Window expressions
  • Slew of minor features

URL()

PATH()

urlpatterns = [

    url(r'^blog/$', views.blog_list),

    url(r'^blog/(?P<pk>\d+)/$', views.blog_detail)

]

urlpatterns = [

    path('blog/', views.blog_list),

    path('blog/entry/<int:pk>/', views.blog_detail)

]

PATH()


path('blog/', views.blog_list)



# str - matches any string, excluding the backslash '/'
path('blog/entry/<title>/', views.blog_detail)

path('blog/entry/<str:title>/', views.blog_detail)

     


# int - matches any zero or positive integer
path('blog/entry/<int:pk>/', views.blog_detail)

    


# slug - matches any string with hyphens
path('blog/entry/<slug:title>/', views.blog_detail)

    



PATH Convertors

  • str, int, slug as already seen before
  • uuid - Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.
  • path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.

Window Expressions

from django.db import models


class Product(models.Model):

    name = models.CharField(max_length=254)
    category = models.ForeignKey('Category', on_delete=models.PROTECT)
    price = models.PositiveSmallIntegerField(default=0)  # stored in pennies

Window Expressions

from django.db.models import Avg, F, Window


Product.objects.annotate(
    avg_price=Window(
        expression=Avg('price'),
        partition_by=F('category'),
        order_by=F('price').desc(),
    ),
)

Window Expressions

name category price avg_price
Apple Fruit 200 156.666667
Pear Fruit 150 156.666667
Orange Fruit 120 156.666667
Tomato Vegetable 600 355.000000
Cucumber Vegetable 110 355.000000

Minor Features

django.contrib.admin

django.contrib.auth

django.contrib.gis

django.contrib.postgres

Management Commands

Full List

 

Django 2.0

By Robert Roskam

Django 2.0

  • 215