The last common pattern we'll look at involves doing some extra work before or after calling the generic view. Imagine we had a last_accessed
field on our Author
model that we were using to keep track of the last time anybody looked at that author:
# models.py from django.db import models class Author(models.Model): salutation = models.CharField(max_length=10) name = models.CharField(max_length=200) email = models.EmailField() headshot = models.ImageField(upload_to='author_headshots') last_accessed = models.DateTimeField()
The generic DetailView
class, of course, wouldn't know anything about this field, but once again we could easily write a custom view to keep that field updated. First, we'd need to add an author detail bit in the URLconf to point to a custom view:
from django.conf.urls import url from books.views import AuthorDetailView urlpatterns = [ #... ...