Performing extra work
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 ...
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.