Robert Roskam
Engineer Manager at Pantheon
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('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)
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
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(),
),
)
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 |
django.contrib.admin
django.contrib.auth
django.contrib.gis
django.contrib.postgres
Management Commands
By Robert Roskam